Array Element Assignments

  1. In the case of primitive arrays as array element any datatype is allowed which can be implicitly promoted to the declared type.
  2. int [] a = new int[10];

    in this case the following datatypes are allowed as array elements.
    byte, short, int, char.

    a[0] = 10;
    a[1] = 'a';
    byte b = 20;
    a[2] = b;
    a[3] = 10.5;// possible loss of precision. found : double
    required: int
  3. in the case of object arrays as array elements we can provide either declared type object or it’s child class objects.
  4. Number[] n = new Number[6];
    n[0] = new Integer(10);
    n[1] = new Long(10l);
    n[2] = new String("raju"); //Incompatible types found : String
                                                    required : Number.
  5. If we declare an array of interface type we are allowed to provide it’s implementation class object as elements.
  6. Runnable[] = new Runnable[]
    r[0] = new Thread();

Array Variable Assignments

  1. A char element can be promoted as the int element but a char array can’t be prompted to int array.
  2. int [] a = new int[6];
    int [] b = a;
    char[] ch = {'a','b','c'};
    int [] c = ch;//Incompatible types found : char[]
  3. when ever we are assigning one array to another array compiler will check only the types instead of sizes.
  4. int[] a = {10,20,30,40};
    int[] b = {60,70};



int [][] a = new int[3][];
a[0] = new int[4];
a[1] = new int[4][5]; // Incompatible types found: int[][]
required: int[]
a[2] = 10; // Incompatible types found: int
required: int[]