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?

3
  • Yeah, it's perfectly valid. It creates an array, no actual List objects. Commented Sep 5, 2013 at 12:51
  • The only thing created at compile time is byte code. Commented Sep 5, 2013 at 12:57
  • [This answer] may help a little. Anyway what is wrong with creating array for objects that implements many kinds of List's? new List<String>[9] wont work because that is type unsafe since you wont create new List<String>[9] but (because of type erasure) new List[9] so you will be able to add to it even ArrayList<Integer> 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. Commented Sep 5, 2013 at 13:01

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Why is it legal then List<String>[] strings = new List[9];? Why would be legal in that circumstance to parameterize the left bit of the assignment?
If it was illegal it would be impossible to implement certain programs, so the compiler lets you off with a warning.
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

Comments

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

Comments

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.

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.