1

Does array creation happen at compilation time?

List<String>[] strings = new List[9];

It works even if List is only an interface, so I guess the array creation happens before type erasue. That's why the following does not work:

   List<String>[] strings = new List<String>[9];

Is it due to the fact that the creation of the array itself takes place prior to type erasure right?

Kara
  • 5,650
  • 15
  • 48
  • 55
Rollerball
  • 11,004
  • 22
  • 81
  • 136
  • Yeah, it's perfectly valid. It creates an array, no actual `List` objects. – Marcin Łoś Sep 05 '13 at 12:51
  • The only thing created at compile time is byte code. – Peter Lawrey Sep 05 '13 at 12:57
  • [[This answer](http://stackoverflow.com/a/18581313/1393766)] may help a little. Anyway what is wrong with creating array for objects that implements many kinds of List's? `new List[9]` wont work because that is type unsafe since you wont create `new List[9]` but (because of type erasure) `new List[9]` so you will be able to add to it even `ArrayList` as mentioned in link I gave earlier. So no, creating array at compilation time is not responsible for that behaviour - and is not possible in Java. – Pshemo Sep 05 '13 at 13:01

4 Answers4

6

Arrays are created in runtime, after type erasure. The newarray bytecode instruction is reserved for creating arrays.

Since an array in Java only holds object references, not concrete objects, you can create arrays of interfaces and abstract classes. No instances are created to fill the array.

As to why you can't create arrays of parametrized types, the reason is that arrays are not typesafe, as explained in the tutorial.

Joni
  • 101,441
  • 12
  • 123
  • 178
  • Why is it legal then `List[] strings = new List[9];`? Why would be legal in that circumstance to parameterize the left bit of the assignment? – Rollerball Sep 05 '13 at 13:37
  • 1
    If it was illegal it would be impossible to implement certain programs, so the compiler lets you off with a warning. – Joni Sep 05 '13 at 13:46
0

Arrays are created at runtime. You are just creating an array that contains List<String> and no actual objects of List<String> are being created. So it is perfectly valid.

Run-time Evaluation of Array Creation Expressions JLS the array creation expressions are evaluated at runtime.

Narendra Pathai
  • 38,384
  • 18
  • 73
  • 117
0

No, arrays are created at runtime. The first snippet works just as List<String> strings = new ArrayList() - raw type is legal for the sake of backwards compatibility, merely discouraged. The second does not work, because well, arrays and generics don't mix well. You can have the whole story here

Marcin Łoś
  • 3,038
  • 1
  • 15
  • 20
0

According to JLS-

In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2).

ArrayCreationExpression:

new PrimitiveType DimExprs Dimsopt
new ClassOrInterfaceType DimExprs Dimsopt
new PrimitiveType Dims ArrayInitializer 
new ClassOrInterfaceType Dims ArrayInitializer
Sajal Dutta
  • 7,258
  • 25
  • 33