Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays are reified in Java

Tags:

java

arrays

I recently came across that, Arrays are reified in Java. That is, they know the type information only during run time. But I am a little confused with this definition.

If Arrays are said to know the type information only during runtime, I should literally be able to assign any values to any arrays, since the typing is known only at run time and errors will be thrown at run time only. But that is not the case in real time. We get a compile time error for that.

So can someone throw light on "what does it mean by - arrays are reified"?

like image 649
shar Avatar asked May 13 '13 09:05

shar


1 Answers

What I think that means is that the given lines of code will throw an exception:

String[] arrayOfStrings = new String[10];
Object[] arrayOfObjects = arrayOfStrings; // compiles fine
arrayOfObjects[0] = new Integer(2); // throws a runtime exception (ArrayStoreException IIRC)

Arrays are covariant: String[] extends Object[]. But the actual type of the array is known at runtime, and an attempt to store an instance which is not of the right type throws an exception.

like image 102
JB Nizet Avatar answered Sep 18 '22 11:09

JB Nizet