It compiles & runs fine! And.... It's said that in K&B's book,
When you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to. ...
What is the differences? Any suggestions?
This message was edited 1 time. Last update was at by Bear Bibeault
Yes Wouter is correct. Array is itself an object , so that is the reason why the following is legal:
or
So it is also legal
Object[] o = new int[] {1,2,3}; //case 1
and
Object[] o = new int[][]{{1,2}.{3.4,5}};//case 2
case 1 : an array is created with 3 Object type reference variables, but the actual instances are int in them.so each Object instance contains one int .
case2: here an array is created with 2 object reference variable s, but the actual instances are int array. So each Object reference of the array contains an int array , but since the reference is single dimensional , the compiler does not permit you to search beyond that.That is why you are getting a compiler error.
case 1 : an array is created with 3 Object type reference variables, but the actual instances are int in them.so each Object instance contains one int .
I think, it's a primitive array, of course, the array is a object! But it holds the actual int primitives...??// Correct??/
Since the array is a object (also extends Object), we can upcast it & can be referenced by a object reference variable. For our purposes later, Can we down cast it to behave as array???
Since the array is a object (also extends Object), we can upcast it & can be referenced by a object reference variable. For our purposes later, Can we down cast it to behave as array?
Yes i think we can downcast it , because the following code works fine , where i tried to downcast the object reference to array:
Since the array is a object (also extends Object), we can upcast it & can be referenced by a object reference variable. For our purposes later, Can we down cast it to behave as array?
Yes i think we can downcast it , because the following code works fine , where i tried to downcast the object reference to array:
yes you can downcast it
but you have to cast it into a proper type
after int[] i = new int[]{1,2,3};
Object o = i;
if you use o.length; or perform any operation using 'o' instead of 'i'.It won't compile.
that's why you have to cast it to int[]
that's why
int[] a = (int[])o;
for (int in:a)
System.out.println(in);
works fine.
Object[] o = new int[] {1,2,3}; //case 1
case 1 : an array is created with 3 Object type reference variables, but the actual instances are int in them.so each Object instance contains one int .
[/code]
Object[] o = new int[] {1,2,3}; is giving me a compilation error.
incompatible types
found : int[]
required: java.lang.Object[]
Object[] o = new int[] {1,2,3};
Please correct if I have missed something and why is it illegal?. Thank You.
Abimaran, the array hierarchy looks something like this
There is a reason for this hierarchy being this way. int[] is a reference to an array and Object[] is an array of references. Thus int[] is not assignable to Object[], as int[] is not an array of references. int[][] can be considered a 2-D int array, or better as an array of int arrays. Thus int[][] becomes an array of references (here the references are 1-D int arrays). This is why int[][] is assignable to Object[]. String[] is an array of references, so it is assignable to Object[]. String[] is an array of references of type String, so String[][] is not assignable to it, as String[][] is an array of references but the references are themselves arrays of Strings. Object[][] is also an array of arrays, so it can point to String[][]. The special case with an Object array is that it can store references of any type. So Object[] may have elements which are Strings, Integers, String array, Integer array, array of String arrays etc. This is why String[], String[][], String[][][] are all assignable to Object[]. So the rule in the K&B book which says
When you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to.
is not applicable to Object[] as it is free to point to an array of any number of dimensions...
(I hope this makes sense, because it didn't make much sense even to me when I wrote it )