In the Java programming language, the wildcard <code>?</code> is a special kind of type argument that controls the type safety of the use of generic (parameterized) types. It can be used in variable declarations and instantiations as well as in method definitions, but not in the definition of a generic type. This is a form of use-site variance annotation, in contrast with the definition-site variance annotations found in C# and Scala.
Unlike arrays (which are covariant in Java), different instantiations of a generic type are not compatible with each other, not even explicitly. For example, the declarations
will cause the compiler to report conversion errors for both castings <code>(Generic<Subtype>)superGeneric</code> and <code>(Generic<Supertype>)subGeneric</code>.
This incompatibility can be softened by the wildcard if <code>?</code> is used as an actual type parameter. <code>Generic<?></code> is a supertype of all parameterizarions of the generic type <code>Generic</code>. This allows objects of type <code>Generic<Supertype></code> and <code>Generic<Subtype></code> to be safely assigned to a variable or method parameter of type <code>Generic<?></code>. Using <code>Generic<? extends Supertype></code> allows the same, restricting compatibility to <code>Supertype</code> and its children. Another possibility is <code>Generic<? super Subtype></code>, which also accepts both objects and restricts compatibility to <code>Subtype</code> and all its parents.
In the body of a generic unit, the (formal) type parameter is handled like its upper bound (expressed with <code>extends</code>; <code>Object</code> if not constrained). If the return type of a method is the type parameter, the result (e.g. of type <code>?</code>) can be referenced by a variable of the type of the upper bound (or <code>Object</code>). In the other direction, the wildcard fits no other type, not even <code>Object</code>: If <code>?</code> has been applied as the formal type parameter of a method, no actual parameters can be passed to it. However, objects of the unknown type can be read from the generic object and assigned to a variable of a supertype of the upperbound.
Sample code for the <code>Generic<T extends UpperBound></code> class:
Sample code that uses the <code>Generic<T extends UpperBound></code> class:
A bounded wildcard is one with either an upper or a lower inheritance constraint. The bound of a wildcard can be either a class type, interface type, array type, or type variable. Upper bounds are expressed using the <code>extends</code> keyword and lower bounds using the <code>super</code> keyword. Wildcards can have at most one bound - either an upper bound or a lower bound, but not both.
An upper bound on a wildcard must be a subtype of the upper bound of the corresponding type parameter declared in the corresponding generic type. An example of a wildcard that explicitly states an upper bound is:
<code>Generic<? extends SubtypeOfUpperBound> referenceConstrainedFromAbove;</code>
This reference can hold any parameterization of <code>Generic</code> whose type argument is a subtype of <code>SubtypeOfUpperBound</code>. A wildcard that does not explicitly state an upper bound is effectively the same as one that has the constraint <code>extends Object</code>, since all reference types in Java are subtypes of Object.
A wildcard with a lower bound, such as
<code>Generic<? super SubtypeOfUpperBound> referenceConstrainedFromBelow;</code>
can hold any parameterization of <code>Generic</code> whose any type argument is both a subtype of the corresponding type parameter's upper bound and a supertype of <code>SubtypeOfUpperBound</code>.
No objects may be created with a wildcard type argument: for example, <code>new Generic<?>()</code> is forbidden. In practice, this is unnecessary because if one wanted to create an object that was assignable to a variable of type <code>Generic<?></code>, one could simply use any arbitrary type (that falls within the constraints of the wildcard, if any) as the type argument.
However, <code>new ArrayList<Generic<?>>()</code> is allowed, because the wildcard is not a parameter to the instantiated type <code>ArrayList</code>. The same holds for <code>new ArrayList<List<?>>()</code>.
In an array creation expression, the component type of the array must be reifiable as defined by the Java Language Specification, Section 4.7. This entails that, if the component type of the array has any type arguments, they must all be unbounded wildcards (wildcards consisting of only a <code>?</code>). For example, <code>new Generic<?>[20]</code> is correct, while <code>new Generic<SomeType>[20]</code> is not.
For both cases, using no parameters is another option. This will generate a warning since it is less type-safe (see Raw type).
In the Java Collections Framework, the class <code>List<MyClass></code> represents an ordered collection of objects of type <code>MyClass</code>. Upper bounds are specified using <code>extends</code>: A <code>List<? extends MyClass></code> is a list of objects of some subclass of <code>MyClass</code>, i.e. any object in the list is guaranteed to be of type <code>MyClass</code>, so one can iterate over it using a variable of type <code>MyClass</code>
However, it is not guaranteed that one can add any object of type <code>MyClass</code> to that list:
The converse is true for lower bounds, which are specified using <code>super</code>: A <code>List<? super MyClass></code> is a list of objects of some superclass of <code>MyClass</code>, i.e. the list is guaranteed to be able to contain any object of type <code>MyClass</code>, so one can add any object of type <code>MyClass</code>:
However, it is not guaranteed that one can iterate over that list using a variable of type <code>MyClass</code>:
In order to be able to do both add objects of type <code>MyClass</code> to the list and iterate over it using a variable of type <code>MyClass</code>, a <code>List<MyClass></code> is needed, which is the only type of <code>List</code> that is both <code>List<? extends MyClass></code> and <code>List<? super MyClass></code>.
The mnemonics PECS (Producer Extends, Consumer Super) from the book Effective Java by Joshua Bloch gives an easy way to remember when to use wildcards (corresponding to Covariance and Contravariance) in Java.
In C++, generic type constraints can be expressed using concepts.
This example is equivalent to the following Java code:
In C#, type bounds can be represented as and (compared to Java and respectively) while a generic type constraint is expressed with a <code>where</code> clause. These are more expressive and powerful than Java wildcards.
Kotlin, though also a JVM language, does not support Java-style type wildcards. However, it represents <code>?</code> instead represented as <code>*</code> (for example, <code>List<*></code>), as well as type bounds as and (compared to Java and respectively). It otherwise has C#-style <code>where</code> clauses:
Rust also uses <code>where</code> clauses to bound traits.