Summary of Generic Types in Java
6 03 2008Generic type := type with formal type parameters.
The type is not a Throwable or a subclass of Throwable, an enum types, or an anonymous inner class.
Type parameter := a place holder for a type argument.
Parameterized type := instantiation of a generic tpye with actual type arguments.
The compiler emits “unchecked” warnings for every dynamic cast whose target type is a parameterized type.
Concrete parameterized type := a parameterized type, where all type arguments are concrete types rather than wildcards, like List<Integer>, but not List<? extends Number>,
:= concrete instantiation of a generic type.
Raw type := generic type without any type arguments.
Generic method := method with type parameters, can usually be called as a regular method.
Type erasure: a parameterized type has no information about the actual type argument, i.e. about the type of the actual type argument.
All instantiations of a generic type, i.e. all parameterized types share the same runtime tpye.
Static members: exist once per enclosing type, not per parameterized type.
Concrete parameterized type
Because of type erasure a concreate parameterized type has no exact runtime type representation.
A concrete parameterized types cannot be used:
- in exception handling
- for the creation of arrays
- in an instanceof expression
- in a class literal.
Reference variables of arrays of concrete parameterized types are possible, but not useful, e.g. List<String>[] x, because no such arrays can be created and there such a variable cannot reference such an array.
Instead use:
- arrays of unbounded wildcard parameterized types, or
- arrays of raw types, or
- collections of concrete parameterized types.
There is no class literal for a concrete parameterized type.
Different instantiations of the same generic type for different concrete type arguments have no type (inheritance) relationship, i.e. List<Object> is not a supertype of List<String>.
Wildcards
? := unbounded wildcard, means all types.
? extends Type := wildcard with an upper bound, means all types that are subtypes of Type, including Type.
? super Type := wildcard with a lower bound; means all types that are supertypes of Type, including Type.
Bounded wildcard := wildcard with an upper or lower bound.
Wildcards used in a type argument list are independent of each other, so the first wildcard refers to a certain type, the second wildcard to another or maybe the same type.
Wildcard bound := reference type which is used as upper or lower bound type in a bounded wildcard. Must be a reference type (including parameterized types), but not a primitive type.
Type parameters
Bounded type parameter := a type parameter with one or more bounds (i.e. with type parameter bounds).
Type parameter bound:
- It is not a type in the common sense.
- It defines what kind of types may be used as the type of a type argument.
- It allows access to the visible methods of the bound type.
- It is visible in the definition of the generic type except in static areas.
- All reference data types are allowed, but no primitiv types or array types.
- Type parameters are allowed as type parameter bounds.
- Only one instantiation of the same generic type is allowed in the list of bounds of a type parameter.
- There are no lower bounds for type parameters.
A type parameter can be used
- for arguments and return types of methods
- for types of fields or local variables
- for type arguments of parameterized types
- for target types in casts
- for excplicit type arguments of parameterized methods
- for throws clauses
A type parameter cannot be used
- for creation of objects
- for creation of arrays
- in catch clauses of exceptions
- in a static context
- in instanceof expressions
- in a class literal
- as supertypes
Type arguments
Type argument := a reference type or a wildcard that is used for the instantiation of a generic type or a reference type used for instantiation of a generic method.
A type argument replaces the formal type parameter used in the declaration of the generic type or method.
It must be within bounds.
Type parameters are permitted as actual type arguments.
Primitive types are not permitted as actual type arguments.
Generic methods cannot be instantiated using wildcards as actual typea rguments.
One can use a generic type without specifying a type argument by using the raw type.
A generic method can be used without type arguments.

Wildcard parameterized type := an instantiation of a generic type where one of the type arguments is a wildcard.
This is not really a type in the common sense.
It can be used:
- for arguments and return types of methods
- for types fields or local variables
- for arrays
- for type arguments of parameterized types
- for target types in casts
It cannot be used:
- for creation of objects
- for creation of arrays (except unbounded wildcard)
- in exception handling
- in instanceof expressions (except unbounded wildcard)
- as supertypes
- in a class literal
Unbounded wildcard parameterized type := an instantiation of a generic type where all type arguments are the unbounded wildcard “?”, e.g. Map<?,?>.
Compiler makes errors for matters concerning unbounded wildcard parameter, but “unchecked” warnings for matters concerning raw types.
An unbounded wildcard parameterized type cannot have subtypes and has no class literal.
It is possible to create an array whose component type is an unbounded wildcard parameterized type.
Object[] maps = new Map<?,?>[ 23 ]; maps[ 0 ] = new Map<Integer,String>();
Differences between wildcard bounds and type parameter bounds
| Wildcard | Type parameter |
| can have only one bound | can have several bounds |
| can have a lower or an upper bound | can have no lower bound |
This summary of generics in Java is based on the impressive work of Angelika Langer.
For further information you may also see the Wikipedia entry.






Recent Comments