Java Reference
In-Depth Information
Forexample, List<E> isagenerictype,where List isaninterfaceandtypepara-
meter E identifies the list's element type. Similarly, Map<K, V> is a generic type,
where Map isaninterfaceandtypeparameters K and V identifythemap'skeyandvalue
Note Whendeclaringagenerictype,itisconventionaltospecifysingleuppercase
example, E indicateselement, T indicatestype, K indicateskey,and V indicatesvalue.If
it is used. For example, List<E> means list of elements, but what does List<S>
Parameterized types instantiate generic types. Each parameterized type replaces the
generic type's type parameters with type names. For example, List<Employee>
( List of Employee )and List<String> ( List of String )areexamplesofpara-
meterized types based on List<E> . Similarly, Map<String, Employee> is an
example of a parameterized type based on Map<K, V> .
The type name that replaces a type parameter is known as an actual type argument .
Generics supports five kinds of actual type arguments:
Concrete type :Thenameofaclassorinterfaceispassedtothetypeparameter.
For example, List<Employee> employees; specifies that the list ele-
ments are Employee instances.
Concrete parameterized type :Thenameofaparameterizedtypeispassedtothe
type parameter. For example, List<List<String>> nameLists; spe-
cifies that the list elements are lists of strings.
Array type : An array is passed to the type parameter. For example,
List<String[]> countries; specifiesthatthelistelementsarearrays
of String s, possibly city names.
Type parameter :Atypeparameterispassedtothetypeparameter.Forexample,
given class declaration class X<E> { List<E> queue; } , X 's type
parameter E is passed to List 's type parameter E .
Wildcard :The ? ispassedtothetypeparameter,indicatinganunknownactual
typeargument.Forexample, List<?> list; specifiesthatthelistelements
are unknown. You will learn about wildcards later in the chapter.
A generic type also identifies a raw type , which is a generic type without its type
parameters.Forexample, List<Employee> 'srawtypeis List .Rawtypesarenon-
generic and can hold any Object .
Search WWH ::

Custom Search