Can someone explain why the last assignment in the code below is not valid
class ScjpTest extends BounceObject implements Bouncable{
static int ac = 5;
static char ab = 'd';
static int[] a = new int[]{1,2,3};
static int[] b = new int[]{1,2,3};
static char[] c = new char[]{'a','b'};
public static void main(String[] args){
a = b;
b = a;
ac = ab; //This is accepted
a = c; //This is rejected
}
}
The compiler complains with the following error
ScjpTest.java:10: incompatible types
found : char[]
required: int[]
a = c;
^
1 error
The following is also accepted
class Animal{}
class Horse extends Animal{]
Animal[] animals = new Animal[2];
Horse[] horses = new Horses[2];
animals = horses;
Why can i then not assign a char array to an int array?
You can assign a char variable to an int one, because the compiler applies a broadening conversion. The basis being that an int is larger than a char and as such there is no chance of loss of information in the conversion. If you tried to assign your int to your char though, you would notice the compiler rejects your code until you put in a specific typecast. This is because narrowing conversions almost always involve a loss of data, and as such the compiler requires you, the programmer, to explicitely indicate this is what you intend to do.
The same principle applies to objects. A broadening conversion is allowed implicitely, a narrowing conversion requires a typecast. Note that at no time can you convert objects that aren’t related to each other by super-class/sub-class hierarchy.
In the example above, Number is a superclass of Integer. Assigning an instance of Integer to instance of Number is allowed without typecast, since this is a ‘broadening’ conversion. Assigning an instance of Number to instance of Integer requires a specific cast since this is a narrowing conversion (Number could have represented a Float, or some other subclass). These rules carry over to arrays. So, you can do the following:
For primitive arrays, there is no broadening conversion done on the array elements, even in cases where you think that it might make sense (ie, char to int, or byte to char etc). While this might not seem to make sense, if you look at the object analogue it becomes clearer why:
You can not cast a Double to an Integer, or vice-versa, they are completely different classes from the point of view of the compiler (as different as String and Float would be, for example).