Reputation: 1639
public class RefMix {
public static void main(String[] args) {
Object[] a = {null, "foo"};
Object[] b = {"bar", b};
a[0] = b;
System.out.println(a[0][0]);
}
}
My understanding is that arrays are objects in Java, and therefore a subclass of the Object type. My further understanding is that a 2-dim array is implemented as an array of references to arrays. Therefore I don't understand why my a[0][0] does not produce bar
in the code above. Instead it doesn't compile:
RefMix.java:7: array required, but java.lang.Object found
Upvotes: 15
Views: 8610
Reputation: 879
As I do a lot of C++ coding lately, I find that Java is a lot more strict in type checking then C++ is. Java sees everything but primitive types as Object but Java goes one step further in distinguishing Array and non-Array types. During assignment like "a[0] = b;" , Java first check to see if it is an Array type or not, after that it goes through regular polymorphic type checking procedure. If you wish to make your code work, you should do...
Object[][] a = {{null}, {"foo"}};
Object[] b = {"bar", new java.util.Date()};
a[0] = b;
You can see how java take special care on array types by looking into Java Class signature that is passed to Class.forName() as parameter. For example, data type ..
com.foo.Bar[][] barsIn2D;
can be loaded with signature below...
// [ => Array
// [[ => Array of Array type
// L<object>; => Object, not Array
Class.forName("[[Lcom/foo/Bar;");
As you see, signature starts with either '[' or 'L'. This tells us whether its an array or not takes precedence over "Lcom/foo/Bar;".
Upvotes: 2
Reputation: 51030
Everything that you are doing is equivalent to this
Object a = {"bar", "foo"};
System.out.println(a[0]);
which doesn't compile either.
Upvotes: 1
Reputation: 9512
You are right, arrays are always Objects
in Java, but Objects
are not always arrays, therefore you get a compile error, because a
is an Object[]
(one dimensional). You can not access
a[0][0];
because a
is not a two dimensional array (at least it's not declared as such). However in this case, you are sure, that a[0]
is an array of Objects
. Therefore, you can do this:
Object[] c = (Object[]) a[0];
System.out.println(c[0]);
// or directly:
System.out.println(((Object[])a[0])[0]);
This casts the return type of a[0]
(which is Object
), into a Object[]
and you can then access the "second layer" of the array.
Upvotes: 7
Reputation: 83527
My understanding is that arrays are objects in Java, and therefore a subclass of the Object type. My further understanding is that a 2-dim array is implemented as an array of references to arrays.
This is all correct and explains why you can do the assignment
a[0] = b;
without any complaints from the compiler.
Therefore I don't understand why my a[0][0] does not produce bar in the code above.
Okay, let's take a look at the types in this expression:
a
is a Object[]
-- that is an array of Object
sa[0]
is an Object
a[0][0]
-- Now you are trying to use an array subscript on an Object
. The compiler does not know that the Object
is in fact an array, so it complains.Upvotes: 13
Reputation: 4264
Do:
System.out.println(((Object[])a[0])[0]);
This will "cast" the object to an object array at runtime.
Upvotes: 4
Reputation: 213223
Since array in Java is an object, so the 1st and 2nd assignment, will store your array as the 1st element of your Object array..
Object[] a = {null, "foo"};
Object[] b = {"bar", b};
Now, you have changed your 1st element of a
object array to contain value b instead of array.. But since it is an array of object. Everything coming out of it will be object..
Since a[0] is an object.. So, you clearly can't access something like this: -
System.out.println(a[0][0]);
You can try to typecast your object a[0] to object array..: -
System.out.println(((Object[])a[0])[0]);
Upvotes: 4
Reputation: 39451
The runtime type of an object instance differs from the statically inferred type. The compiler will try to estimate what type each variable could be in the program, in order to catch certain types of errors early. In this case, a[0]
will always be an array, but the compiler doesn't know this. Since you are getting an object out of an object array, all the compiler knows is that a[0]
is an object. Therefore it raises an error.
In cases where you know something will always be a certain type but the compiler can't figure it out, you can get around this by inserting an explicit cast.
System.out.println(((Object[])a[0])[0]);
Upvotes: 10