-
Notifications
You must be signed in to change notification settings - Fork 0
Description
No, because it is not type-safe.
Arrays are covariant, so Object[] is a supertype of String[] and a string array can be accessed through a reference variable of type Object[].
Example of covariant arrays:
Object[] objArr = new String[10]; //fine
objArr[0] = new String();The runtime type information regarding the component type is used when elements are stored in an array in order to ensure that no "alien" elements can be inserted.
Example of array store check:
Object[] objArr = new String[10];
objArr[0] = new Long(0L); //Compiles; fails at runtime with ArrayStoreExceptionThe reference variable of type Object[] refers to a String[], which means that only strings are permitted as elements of the array. When an element is inserted into the array, the information about the array's component type is used to perform a type check - the so-called array store check.
Problems arise when an array holds elements whose type is a concrete parameterized type. Because of type erasure, parameterized types do not have exact runtime type information. As a consequence, the array store check does not work because it uses the dynamic type information regarding the array's (non-exact) component type for the array store check.
Example of array store check in case of parameterized component type:
Pair<Integer, Integer>[] intPairArr = new Pair<Integer, Integer>[10]; //illegal
Object[] objArr = intPairArr;
objArr[0] = new Pair<String, String>(","); //should fail, but would succeedSince we are trying to add a Pair<String, String> to a Pair<Integer, Integer>[] we would expect that the type check fails. However, the JVM cannot detect any type mismatch here: **at runtime, after type erasure, objArr would be have the dynamic type Pair[] and the element to be stored has the matching dynamic type Pair. Hence the store check succeeds, although it should not.
In order to prevent programs that are not type-safe all arrays holding elements whose type is a concrete parameterized type are illegal. For the same reason, arrays holding elements whose type is a wildcard parameterized type are banned, too. Only arrays with an unbounded wildcard parameterized type as the component type are permitted.