Java Reference
In-Depth Information
The fourth line assigns empListArray to objArray . This assignment is legal
becausearraysarecovariantanderasureconverts List<Employee>[] tothe List
runtime type, and List subtypes Object .
Because oferasure,theJVMdoesn'tthrow ArrayStoreException wheniten-
counters objArray[0] = strList; .Afterall,we'reassigninga List reference
to a List[] array at runtime. However, this exception would be thrown if generic
types were reified because we'd then be assigning a List<String> reference to a
List<Employee>[] array.
However,thereisaproblem.A List<String> instancehasbeenstoredinanarray
thatcanonlyhold List<Employee> instances.Whenthecompiler-insertedcastop-
erator attempts to cast empListArray[0].get(0) 's return value ( "string" ) to
Employee , the cast operator throws a ClassCastException object.
cify array-creation expressions that involve type parameters or actual type arguments.
Varargs and Generics
When you invoke a varargs (variable umber of arguments) method whose parameter
is declared to be a parameterized type (as in List<String> ), the compiler emits a
warning message at the point of call. This message can be confusing and tends to dis-
courage the use of varargs in third-party APIs.
Thewarningmessageisrelatedto heap pollution ,whichoccurswhenavariableofa
unchecked warning at compile time. ( The Java Language Specification, Third Edition
jls/third_edition/html/typesValues.html# ] ).
Unchecked warnings occur in calls to varargs methods whose parameter types are
pressed at runtime because of erasure.
element type is stored internally and used when required for various runtime type
checks. However, this stored type information cannot include information required to
represent a parameterized type that is onreifiable.
Search WWH ::

Custom Search