Reputation: 2679
Why does this code compile?
Object[] object = new String[5][5];
I mean, why can I do that if I'm creating an array object with different dimensions than specified in the reference variable?
This doesn't compile:
String[] strings = new String[5][5];
So what is happening here?
Upvotes: 9
Views: 2695
Reputation: 41097
The first one compiles because String[]
is an Object
. The 2nd one doesn't compiles because String
is not String[]
.
Object[] object = new String[5][5]; // Means each element is an String[] which is an Object as well.
String[] strings = new String[5][5]; // Also Means each element is an String[] which is not same as just String.
Upvotes: 4
Reputation: 3036
1- for the first code you can't be able to use string methods as
object[0][0].substring(1);//error
//correct to use string methods
strings[0][0].substring(1);
strings[0][0].chartAt(0);
but the second code you can use string methods.
2- second code must be as:
String[][] strings = new String[5][5];
Upvotes: 0
Reputation: 2672
An array of Strings (so String[5] for example) can be considered as an Object. Same for two-dimensional array (or array of arrays). However, a double-dimensional array of Strings is not a single-dimensional array of Strings, so you can't assign 1-dim array of Strings to 2-dim array of Strings.
Also notice, that in Java (and not only) a two-dimensional array is basically an array of arrays. Therefore in the example you gave, you can assign a two-dimensional array (an array of arrays) of Strings to an array of Objects (so at every index of the Object array you store a single-dimensional array of Strings), but you can't do it with Strings.
Upvotes: 1
Reputation: 234807
Arrays in Java are covariant. For any types T1
and T2
, if T2
derives from T1
(that is, T2
directly or indirectly extends or implements T1
), then T2[]
is a subtype of T1[]
. Thus, String[]
is a subtype of Object[]
and you can assign an object of type String[]
to a variable of type Object[]
.
Note (as Oli Charlesworth points out in the comment), covariance breaks Java's compile-time type safety. This code:
Object [] o = new String[5];
o[0] = Integer.valueOf(3);
will generate an ArrayStoreException
at run time when the second line tries to execute. So I'm not suggesting that covariant arrays are a great thing; it's just that's how the language works.
Regarding your second example, a String[]
is not a String[][]
. Covariance does not apply because String[]
does not derive from String
. However, you could do:
Object[] o = new String[5][5];
because a String[]
is, in fact, an Object
.
Upvotes: 4
Reputation: 22181
Any array is itself an Object
.
Thus, by this rule:
String[5]
is an Object
.
String[5][]
is an Object[]
.
String[5][]
and String[5]
are Object
s too.
The distinction matters when one wants to enforce compiler to deal with an array or multi-array, but not a simple Object
.
For all types other than Object
, this rule does not apply and then:
String[5][5]
IS NOT a String[]
Upvotes: 1