A class or interface that is generic (§8.1.2, §9.1.2) defines a set of parameterized types.
A parameterized type is a class or interface type of the form C<T_1,...,T_n>, where C is the name of a generic class or interface, and <T_1,...,T_n> is a list of type arguments that denote a particular parameterization of the generic class or interface.
A generic class or interface has type parameters F_1,...,F_n with corresponding bounds B_1,...,B_n. Each type argument T_i of a parameterized type ranges over all types that are subtypes of all types listed in the corresponding bound. That is, for each bound type S in B_i, T_i is a subtype of S[F_1:=T_1,...,F_n:=T_n] (§4.10).
A parameterized type C<T_1,...,T_n> is well-formed if all of the following are true:
- C is the name of a generic class or interface.
- The number of type arguments is the same as the number of type parameters in the generic declaration of C.
- When subjected to capture conversion (§5.1.10) resulting in the type C
<X_1,...,X_n>, each type argument X_i is a subtype of S[F_1:=X_1,...,F_n:=X_n]for each bound type S in B_i.
It is a compile-time error if a parameterized type is not well-formed.
In this specification, whenever we speak of a class or interface type, we include parameterized types as well, unless explicitly excluded.
Two parameterized types are provably distinct if either of the following is true:
- They are parameterizations of distinct generic type declarations.
- Any of their type arguments are provably distinct.
Given the generic classes in the examples of §8.1.2, here are some well-formed parameterized types:
Seq<String>Seq<Seq<String>>Seq<String>.Zipper<Integer>Pair<String,Integer>
Here are some incorrect parameterizations of those generic classes:
Seq<int>is illegal, as primitive types cannot be type arguments.Pair<String>is illegal, as there are not enough type arguments.Pair<String,String,String>is illegal, as there are too many type arguments.
A parameterized type may be a parameterization of a generic class or interface which is nested. For example, if a non-generic class C has a generic member class D with one type parameter, then C.D<Object> is a parameterized type. Meanwhile, if a generic class C with one type parameter has a non-generic member class D, then the member class type C<String>.D is a parameterized type, even though the class D is not generic.
4.5.1. Type Arguments of Parameterized Types
Type arguments may be either reference types or wildcards. Wildcards are useful in situations where only partial knowledge about the type parameter is required.
TypeArguments:
< TypeArgumentList >
TypeArgumentList:
TypeArgument {, TypeArgument}
TypeArgument:
ReferenceType
Wildcard
Wildcard:
{Annotation} ? [WildcardBounds]
WildcardBounds:
extends ReferenceType
super ReferenceType
Wildcards may be given explicit bounds, just like regular type variable declarations. An upper bound is signified by the following syntax, where B is the bound:
? extends B
Unlike ordinary type variables declared in a method signature, no type inference is required when using a wildcard. Consequently, it is permissible to declare lower bounds on a wildcard, using the following syntax, where B is a lower bound:
? super B
The wildcard ? extends Object is equivalent to the unbounded wildcard ?.
Two type arguments are provably distinct if one of the following is true:
- Neither argument is a type variable or wildcard, and the two arguments are not the same type.
- One type argument is a type variable or wildcard, with an upper bound (from capture conversion (§5.1.10), if necessary) of S; and the other type argument T is not a type variable or wildcard; and neither |S|
<:|T| nor |T|<:|S| (§4.8, §4.10). - Each type argument is a type variable or wildcard, with upper bounds (from capture conversion, if necessary) of S and T; and neither |S|
<:|T| nor |T|<:|S|.
A type argument T_1 is said to contain another type argument T_2, written T_2 <= T_1, if the set of types denoted by T_2 is provably a subset of the set of types denoted by T_1 under the reflexive and transitive closure of the following rules (where <: denotes subtyping (§4.10)):
?extendsT<=?extendsS if T<:S?extendsT<=??superT<=?superS if S<:T?superT<=??superT<=?extendsObject- T
<=T - T
<=?extendsT - T
<=?superT
The relationship of wildcards to established type theory is an interesting one, which we briefly allude to here. Wildcards are a restricted form of existential types. Given a generic type declaration G<T extends B>, G<?> is roughly analogous to Some X <: B. G<X>.
Historically, wildcards are a direct descendant of the work by Atsushi Igarashi and Mirko Viroli. Readers interested in a more comprehensive discussion should refer to On Variance-Based Subtyping for Parametric Types by Atsushi Igarashi and Mirko Viroli, in the Proceedings of the 16th European Conference on Object Oriented Programming (ECOOP 2002). This work itself builds upon earlier work by Kresten Thorup and Mads Torgersen (Unifying Genericity, ECOOP 99), as well as a long tradition of work on declaration based variance that goes back to Pierre America's work on POOL (OOPSLA 89).
Wildcards differ in certain details from the constructs described in the aforementioned paper, in particular in the use of capture conversion (§5.1.10) rather than the close operation described by Igarashi and Viroli. For a formal account of wildcards, see Wild FJ by Mads Torgersen, Erik Ernst and Christian Plesner Hansen, in the 12th workshop on Foundations of Object Oriented Programming (FOOL 2005).
Example 4.5.1-1. Unbounded Wildcards
import java.util.ArrayList;
import java.util.Collection;
class Test {
static void printCollection(Collection<?> c) {
// a wildcard collection
for (Object o : c) {
System.out.println(o);
}
}
public static void main(String[] args) {
Collection<String> cs = new ArrayList<String>();
cs.add("hello");
cs.add("world");
printCollection(cs);
}
}
Note that using Collection<Object> as the type of the incoming parameter, c, would not be nearly as useful; the method could only be used with an argument expression that had type Collection<Object>, which would be quite rare. In contrast, the use of an unbounded wildcard allows any kind of collection to be passed as an argument.
Here is an example where the element type of an array is parameterized by a wildcard:
public Method getMethod(Class<?>[] parameterTypes) { ... }
Example 4.5.1-2. Bounded Wildcards
boolean addAll(Collection<? extends E> c)
Here, the method is declared within the interface Collection<E>, and is designed to add all the elements of its incoming argument to the collection upon which it is invoked. A natural tendency would be to use Collection<E> as the type of c, but this is unnecessarily restrictive. An alternative would be to declare the method itself to be generic:
<T> boolean addAll(Collection<T> c)
This version is sufficiently flexible, but note that the type parameter is used only once in the signature. This reflects the fact that the type parameter is not being used to express any kind of interdependency between the type(s) of the argument(s), the return type and/or throws type. In the absence of such interdependency, generic methods are considered bad style, and wildcards are preferred.
Reference(T referent, ReferenceQueue<? super T> queue)
Here, the referent can be inserted into any queue whose element type is a supertype of the type T of the referent; T is the lower bound for the wildcard.
4.5.2. Members and Constructors of Parameterized Types
Let C be a generic class or interface with type parameters A_1,...,A_n, and let C<T_1,...,T_n> be a parameterization of C where, for 1 ≤ i ≤ n, T_i is a type (rather than a wildcard). Then:
-
Let
mbe a member or constructor declaration in C, whose type as declared is T (§8.2, §8.8.6).The type of
min C<T_1,...,T_n>is T[A_1:=T_1,...,A_n:=T_n]. -
Let
mbe a member or constructor declaration in D, where D is a class extended by C or an interface implemented by C. Let D<U_1,...,U_k>be the supertype (§4.10.2) of C<T_1,...,T_n>that corresponds to D.The type of
min C<T_1,...,T_n>is the type ofmin D<U_1,...,U_k>.
If any of the type arguments in the parameterization of C are wildcards, then:
- The types of the fields, methods, and constructors in C
<T_1,...,T_n>are the types of the fields, methods, and constructors in the capture conversion of C<T_1,...,T_n>(§5.1.10). - Let D be a (possibly generic) class or interface declaration in C. Then the type of D in C
<T_1,...,T_n>is D where, if D is generic, all type arguments are unbounded wildcards.
This is of no consequence, as it is impossible to access a member of a parameterized type without performing capture conversion, and it is impossible to use a wildcard after the keyword new in a class instance creation expression (§15.9).
The sole exception to the previous paragraph is when a nested parameterized type is used as the expression in an instanceof operator (§15.20.2), where capture conversion is not applied.
A static member that is declared in a generic class or interface must be referred to using the name of the generic class or interface (§6.1, §6.5.5.2, §6.5.6.2), or a compile-time error occurs.
In other words, it is illegal to refer to a static member declared in a generic type declaration by using a parameterized type.