Interesting facts about Array assignment in Java - GeeksforGeeks

06 Jan, 2019
Prerequisite : Arrays in Java
While working with arrays we have to do 3 tasks namely declaration, creation, initialization or Assignment.Declaration of array :
int[] arr;
Creation of array :
// Here we create an array of size 3
int[] arr = new int[3];
Initialization of array :
arr[0] = 1;
arr[1] = 2;
arr[3] = 3;

int intArray[];    // declaring array
intArray = new int[20];  // allocating memory to array
Some important facts while assigning elements to the array:
For primitive data types : In case of primitive type arrays, as array elements we can provide any type which can be implicitly promoted to the declared type array. Apart from that, if we are trying to use any other data-types then we will get compile-time error saying possible loss of precision.// Java program to illustrate the concept of array// element assignments on int type arraypublic class Test {public static void main(String[] args)    {        int[] arr = new int[3];        arr[0] = 1;        arr[1] = 'a';        byte b = 10;        arr[2] = b;        System.out.println(arr[0] + arr[1] + arr[2]);    }}Output:108
// Java program to illustrate the concept of array// element assignments on int type arraypublic class Test {public static void main(String[] args)    {        int[] arr = new int[3];          // Assigning long value to int type.        arr[0] = 10l;        arr[1] = 'a';        byte b = 10;        arr[2] = b;          System.out.println(arr[0] + arr[1] + arr[2]);    }}Output:possible loss of precision.
// Java program to illustrate the concept of array// element assignments on char type arraypublic class Test {public static void main(String[] args)    {        char[][] arr = new char[2][2];          // Assigning long value to int type.        arr[0][0] = 10l;        arr[0][1] = 'a';        char b = 10;        arr[1][0] = b;          // Assigning double value to char type        arr[1][1] = 10.6;    }}Output:error: incompatible types: possible lossy conversion from long to char
error: incompatible types: possible lossy conversion from double to char
Object type Arrays : If we are creating object type arrays then the elements of that arrays can be either declared type objects or it can be child class object.// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[] num = new Number[2];        num[0] = new Integer(10);        num[1] = new Double(20.5);        System.out.println(num[0]);        System.out.println(num[1]);    }}Output:10
20.5
// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[] num = new Number[3];        num[0] = new Integer(10);        num[1] = new Double(20.5);          // Here String is not the child class of Number class.        num[2] = new String(“GFG”);    }}Output:Compile-time error(incompatible types)
// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[][] arr = new Number[2][2];        arr[0][0] = 10l;          // Assigning char to Number type array        arr[0][1] = 'a';        byte b = 10;        arr[1][0] = b;          // Assigning String to Number type array        arr[1][1] = "GEEKS";    }}Output:error: incompatible types: char cannot be converted to Number
error: incompatible types: String cannot be converted to Number
Interface type array : For interface type array, we can assign elements as its implementation class objects.// Java program to illustrate the concept of array// element assignments on Interface type arraypublic class Test {public static void main(String[] args)    {        Runnable[] run = new Runnable[2];          // Thread class implements Runnable interface.        run[0] = new Thread();        run[1] = new Thread();    }}// Java program to illustrate the concept of array// element assignments on Interface type arraypublic class Test {public static void main(String[] args)    {        Runnable[] run = new Runnable[2];        run[0] = new Thread();          // String class does not implements Runnable interface.        run[1] = new String(“GFG”);    }}Output:Compile-time error(Incompatible types)
Explanation: In the above program, we are giving elements of String class that’s cause compile time error. Because we know that String does not implements Runnable interface.
For primitive data types : In case of primitive type arrays, as array elements we can provide any type which can be implicitly promoted to the declared type array. Apart from that, if we are trying to use any other data-types then we will get compile-time error saying possible loss of precision.// Java program to illustrate the concept of array// element assignments on int type arraypublic class Test {public static void main(String[] args)    {        int[] arr = new int[3];        arr[0] = 1;        arr[1] = 'a';        byte b = 10;        arr[2] = b;        System.out.println(arr[0] + arr[1] + arr[2]);    }}Output:108
// Java program to illustrate the concept of array// element assignments on int type arraypublic class Test {public static void main(String[] args)    {        int[] arr = new int[3];          // Assigning long value to int type.        arr[0] = 10l;        arr[1] = 'a';        byte b = 10;        arr[2] = b;          System.out.println(arr[0] + arr[1] + arr[2]);    }}Output:possible loss of precision.
// Java program to illustrate the concept of array// element assignments on char type arraypublic class Test {public static void main(String[] args)    {        char[][] arr = new char[2][2];          // Assigning long value to int type.        arr[0][0] = 10l;        arr[0][1] = 'a';        char b = 10;        arr[1][0] = b;          // Assigning double value to char type        arr[1][1] = 10.6;    }}Output:error: incompatible types: possible lossy conversion from long to char
error: incompatible types: possible lossy conversion from double to char

// Java program to illustrate the concept of array// element assignments on int type arraypublic class Test {public static void main(String[] args)    {        int[] arr = new int[3];        arr[0] = 1;        arr[1] = 'a';        byte b = 10;        arr[2] = b;        System.out.println(arr[0] + arr[1] + arr[2]);    }}
Output:
108

// Java program to illustrate the concept of array// element assignments on int type arraypublic class Test {public static void main(String[] args)    {        int[] arr = new int[3];          // Assigning long value to int type.        arr[0] = 10l;        arr[1] = 'a';        byte b = 10;        arr[2] = b;          System.out.println(arr[0] + arr[1] + arr[2]);    }}
Output:
possible loss of precision.

// Java program to illustrate the concept of array// element assignments on char type arraypublic class Test {public static void main(String[] args)    {        char[][] arr = new char[2][2];          // Assigning long value to int type.        arr[0][0] = 10l;        arr[0][1] = 'a';        char b = 10;        arr[1][0] = b;          // Assigning double value to char type        arr[1][1] = 10.6;    }}
Output:
error: incompatible types: possible lossy conversion from long to char
error: incompatible types: possible lossy conversion from double to char

Object type Arrays : If we are creating object type arrays then the elements of that arrays can be either declared type objects or it can be child class object.// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[] num = new Number[2];        num[0] = new Integer(10);        num[1] = new Double(20.5);        System.out.println(num[0]);        System.out.println(num[1]);    }}Output:10
20.5
// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[] num = new Number[3];        num[0] = new Integer(10);        num[1] = new Double(20.5);          // Here String is not the child class of Number class.        num[2] = new String(“GFG”);    }}Output:Compile-time error(incompatible types)
// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[][] arr = new Number[2][2];        arr[0][0] = 10l;          // Assigning char to Number type array        arr[0][1] = 'a';        byte b = 10;        arr[1][0] = b;          // Assigning String to Number type array        arr[1][1] = "GEEKS";    }}Output:error: incompatible types: char cannot be converted to Number
error: incompatible types: String cannot be converted to Number

// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[] num = new Number[2];        num[0] = new Integer(10);        num[1] = new Double(20.5);        System.out.println(num[0]);        System.out.println(num[1]);    }}
Output:
10
20.5

// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[] num = new Number[3];        num[0] = new Integer(10);        num[1] = new Double(20.5);          // Here String is not the child class of Number class.        num[2] = new String(“GFG”);    }}
Output:
Compile-time error(incompatible types)

// Java program to illustrate the concept of array// element assignments on Number type arraypublic class Test {public static void main(String[] args)    {        Number[][] arr = new Number[2][2];        arr[0][0] = 10l;          // Assigning char to Number type array        arr[0][1] = 'a';        byte b = 10;        arr[1][0] = b;          // Assigning String to Number type array        arr[1][1] = "GEEKS";    }}
Output:
error: incompatible types: char cannot be converted to Number
error: incompatible types: String cannot be converted to Number

Interface type array : For interface type array, we can assign elements as its implementation class objects.// Java program to illustrate the concept of array// element assignments on Interface type arraypublic class Test {public static void main(String[] args)    {        Runnable[] run = new Runnable[2];          // Thread class implements Runnable interface.        run[0] = new Thread();        run[1] = new Thread();    }}// Java program to illustrate the concept of array// element assignments on Interface type arraypublic class Test {public static void main(String[] args)    {        Runnable[] run = new Runnable[2];        run[0] = new Thread();          // String class does not implements Runnable interface.        run[1] = new String(“GFG”);    }}Output:Compile-time error(Incompatible types)
Explanation: In the above program, we are giving elements of String class that’s cause compile time error. Because we know that String does not implements Runnable interface.
// Java program to illustrate the concept of array// element assignments on Interface type arraypublic class Test {public static void main(String[] args)    {        Runnable[] run = new Runnable[2];          // Thread class implements Runnable interface.        run[0] = new Thread();        run[1] = new Thread();    }}
// Java program to illustrate the concept of array// element assignments on Interface type arraypublic class Test {public static void main(String[] args)    {        Runnable[] run = new Runnable[2];        run[0] = new Thread();          // String class does not implements Runnable interface.        run[1] = new String(“GFG”);    }}
Output:
Compile-time error(Incompatible types)

Explanation: In the above program, we are giving elements of String class that’s cause compile time error. Because we know that String does not implements Runnable interface.
Reference : https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html
This article is contributed by Bishal Kumar Dubey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
interesting-facts
Java-Array-Programs
Java-Arrays
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples
Stream In Java
Interfaces in Java
How to iterate any Map in Java
ArrayList in Java
Initialize an ArrayList in Java
Stack Class in Java
Singleton Class in Java
Multidimensional Arrays in Java