1

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?

1
  • 1
    I'm assuming you want class Horse extends Animal{} for your example Commented Aug 8, 2011 at 16:34

3 Answers 3

8

Because the language specification forbids it. It works at runtime to view a String[] as an Object[] because both have the same representation in memory. But char[] and int[] have different memory representations, and it would be unacceptably slow if every operation on an int[] had to check whether it was really a char[] in disguise.

Arrays of reference type do have to do runtime checks for assignments in order to know whether to throw ArrayStoreException, but at least reads from them is independent of the element type.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I can see now why they decided to restrict it :)
4

The array is a type itself, and casting doesn't work the same way as it primitives. So you have to loop all elements and assign them to the respective element of the other array.

Comments

1

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.

Number n = null;
Integer i = null;

n = i ;         // Allowed without casting, Number is superclass of Integer
i = n;          // Compiler error, Integer is sub-class of Number
i= (Integer)n;  // Allowed due to the type-cast

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:

Number[] numArr = null;
Integer[] intArr = null;

numArr = intArr;
intArr = numArr;              //Compile error
intArr = (Integer[]) numArr;

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:

Double[] doubleArr = null;
Integer[] intArr = null;

doubleArr = intArr ;      // Compile error, Double,Integer are sibling classes
doubleArr = (Double[]) intArr;  // Compile error, same reason
intArr = doubleArr;             // Compile error, same reason
intArr = (Integer[]) doubleArr; // Compile error, same reason

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.