9

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?

2
  • 9
    Remember that an array is an Object in Java. Commented Jan 3, 2014 at 0:47
  • Arrays, in this case a String array, are considered Objects too. Try Object obj = new String[],and see how it goes. Commented Jan 3, 2014 at 0:48

5 Answers 5

4

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.
Sign up to request clarification or add additional context in comments.

1 Comment

This was the answer i was looking for
4

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.

2 Comments

Which is fine until you do String[] strings = new String[5]; Object[] objs = strings; objs[0] = new Integer(42);...
@OliCharlesworth - Thanks for that. I added something about it to my answer.
1

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 Objects 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[]

Comments

1

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.

Comments

0

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];

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.