Wildcard (Java)
The wildcard (rarely also a joker ) ?
is a special current type parameter in Java for instantiating generic (parameterized) types . This article summarizes the main rules for its use.
Covariance for generic types
In contrast to arrays (which are covariant in Java ), different instantiations of a generic type are not compatible with each other (not even explicitly): According to the agreements, the compiler reports an error for both conversions (castings) and .
Generisch<Obertyp> oberGenerisch; Generisch<Untertyp> unterGenerisch;
(Generisch<Untertyp>)oberGenerisch
(Generisch<Obertyp>)unterGenerisch
This incompatibility can be softened with the wildcard if the following is used ?
for a current type parameter: is the abstract supertype of all instantiations of the generic type. This means that only references, not objects, can be created of this type. The point of such a reference is that any instantiations of match it.
Generisch<?>
Generisch
Wildcard as a parameter type
In the body of the generic unit, the type parameter is Object
handled like the upper bound (if unrestricted, then how ). If the result type (return type) of a function is the type parameter, the result (e.g. of the type ?
) can be transferred to a reference of the type of the limit ( Object
if there is no limit). In the other direction, no other type fits the wildcard type, not even Object
: If ?
a method has been used for the type of the formal parameter, no current parameters can be passed to it. It can then only be called after converting (casting) the wildcard reference:
class Generisch<T extends Schranke> {
private T t;
void schreiben(T t) { this.t = t; }
T lesen() { return t; }
}
...
Generisch<?> jokerReferenz;
Schranke o = jokerReferenz.lesen(); // Object wäre auch OK
jokerReferenz.schreiben(new Object()); // Typfehler
((Generisch<Schranke>)jokerReferenz).schreiben(new Schranke()); // OK
Wildcard restriction
Not only the formal type parameter, but also the wildcard can be (further) restricted from above if you do not want to keep arbitrary instantiations compatible:
Generisch<? extends UntertypVonSchranke> vonObenEingeschränkteReferenz;
An instance of can now be Generisch
attached to this reference , where the current type parameter is a subtype of UntertypVonSchranke
. In a restriction from below
Generisch<? super UntertypVonSchranke> vonUntenEingeschränkteReferenz;
Generisch
Instantiations of with any supertype (e.g. Schranke
) of can be UntertypVonSchranke
attached. It is therefore possible that the permitted types are restricted from two sides: from above by the class declaration ( ), from below by the reference declaration ( ).
extends Schranke
super UntertypVonSchranke
Object creation with wildcard instantiations
Although objects cannot be created from wildcard instantiations (this is forbidden because it is abstract), array objects can only be created from unrestricted wildcard instantiations (i.e. no other generic instantiations): is correct while is forbidden.
new Generisch<?>()
Generisch<?>
new Generisch<?>[20]
new Generisch<Schranke>[20]
literature
- The Java Language Specification , Third Edition. Sun, ISBN 978-0-321-24678-3
Web links
- Generics . Java tutorials
- Type compatibility in Java
Individual evidence
- ↑ James Gosling et al. a .: Chapter 4. Types, Values, and Variables. 4.5.1. Type arguments and wildcards. In: Java Language Specification. February 28, 2013, accessed January 14, 2013 .