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
This is all correct and explains why you can do the assignment
without any complaints from the compiler.
Okay, let’s take a look at the types in this expression:
ais aObject[]— that is an array ofObjectsa[0]is anObjecta[0][0]— Now you are trying to use an array subscript on anObject. The compiler does not know that theObjectis in fact an array, so it complains.