Java Reference
In-Depth Information
You cannot use a parameterized type in a class literal expression
(such as SingleLinkQueue<String>.class ). This reinforces that there
is but one class object.
The restriction on creating arrays of a parameterized type can be lim-
iting but is essential. Consider, for example, an implementation of List
that provides a split method that returns the contents of the list split
across a number of sub-lists. It would seem reasonable to return the
sublists as an array of lists. So given a class that implements List<T> the
split method would return List<T>[] . You could actually declare such a
method, but you would have trouble implementing it. You cannot cre-
ate an array with component type List<T> , and any attempt to return
some less specific array type (say List[] ) would result in an "unchecked"
warning (see below). The reason you cannot create a generic array such
as List<T>[] is that correct use of the array cannot be enforced by the
type system. This is because array store checks are based on the eras-
ure of the component type not on their actual parameterized type, so
you would be allowed to store any kind of List in an array that is sup-
posed to have a component type of List<String> , for example. Not allow-
ing you to create generic arrays avoids this problem. From the program-
mers perspective, it is better to try to work with a suitable collection of a
parameterized type rather than an array. For example, the split meth-
od could be defined to return List<List<T>> instead.
Using unbounded wildcards in array creation and instanceof is allowed
for parameterized types because such types are effectively equivalent
to their raw types. For example, creating a List<?>[1] is effectively the
same as creating a List[1] an array whose component type is a raw type.
However, there is one significant difference between these two forms:
The raw type permits unsafe operations but causes the compiler to issue
a warning, whereas the wildcard form prohibits unsafe operations, caus-
ing a compile-time error.
The absence of generic type information at runtime also means that
casts involving type parameters or parameterized types may not have
the desired effect: You cannot check whether an object is an instance
of a parameterized type, so a cast to that type can not be checked at
runtimeexcept if all type parameters are unbounded wildcards. In an
 
Search WWH ::




Custom Search