Wildcard (Java)
   HOME

TheInfoList



OR:

In the
Java programming language Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
, the wildcard ? is a special kind of type argument that controls the
type safety In computer science, type safety and type soundness are the extent to which a programming language discourages or prevents type errors. Type safety is sometimes alternatively considered to be a property of facilities of a computer language; that is ...
of the use of
generic Generic or generics may refer to: In business * Generic term, a common name used for a range or class of similar things not protected by trademark * Generic brand, a brand for a product that does not have an associated brand or trademark, other ...
(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.


Covariance for generic types

Unlike arrays (which are covariant in Java), different instantiations of a generic type are not compatible with each other, not even explicitly: With the declaration Generic superGeneric; Generic subGeneric; the compiler would report a conversion error for both castings (Generic)superGeneric and (Generic)subGeneric. This incompatibility may be softened by the wildcard if ? is used as an actual type parameter: Generic is a supertype of all parameterizarions of the generic type Generic. This allows objects of type Generic and Generic to be safely assigned to a variable or method parameter of type Generic. Using Generic allows the same, restricting compatibility to Supertype and its children. Another possibility is Generic, which also accepts both objects and restricts compatibility to Subtype and all its parents.


Wildcard as parameter type

In the body of a generic unit, the (formal) type parameter is handled like its
upper bound In mathematics, particularly in order theory, an upper bound or majorant of a subset of some preordered set is an element of that is greater than or equal to every element of . Dually, a lower bound or minorant of is defined to be an element ...
(expressed with extends; Object if not constrained). If the return type of a method is the type parameter, the result (e.g. of type ?) can be referenced by a variable of the type of the upper bound (or Object). In the other direction, the wildcard fits no other type, not even Object: If ? 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. class Generic ... Generic concreteTypeReference = new Generic(); Generic wildcardReference = concreteTypeReference; UpperBound ub = wildcardReference.read(); // Object would also be OK wildcardReference.write(new Object()); // type error wildcardReference.write(new UpperBound()); // type error concreteTypeReference.write(new UpperBound()); // OK


Bounded wildcards

A bounded wildcard is one with either an upper or a lower
inheritance Inheritance is the practice of receiving private property, Title (property), titles, debts, entitlements, Privilege (law), privileges, rights, and Law of obligations, obligations upon the death of an individual. The rules of inheritance differ ...
constraint. The bound of a wildcard can be either a class type,
interface Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Inte ...
type, array type, or type variable. Upper bounds are expressed using the extends keyword and lower bounds using the super keyword. Wildcards can state either an upper bound ''or'' a lower bound, but not both.


Upper bounds

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: Generic referenceConstrainedFromAbove; This reference can hold any parameterization of Generic whose type argument is a subtype of SubtypeOfUpperBound. A wildcard that does not explicitly state an upper bound is effectively the same as one that has the constraint extends Object, since all reference types in Java are subtypes of Object.


Lower bounds

A wildcard with a lower bound, such as Generic referenceConstrainedFromBelow; can hold any parameterization of Generic whose any type argument is both a subtype of the corresponding type parameter's upper bound and a supertype of SubtypeOfUpperBound.


Object creation with wildcard

No objects may be created with a wildcard type argument: for example, new Generic() is forbidden. In practice, this is unnecessary because if one wanted to create an object that was assignable to a variable of type Generic, one could simply use any arbitrary type (that falls within the constraints of the wildcard, if any) as the type argument. However, new ArrayList>() is allowed, because the wildcard is not a parameter to the instantiated type ArrayList. The same holds for new ArrayList>(). 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 ?) . For example, new Generic 0/code> is correct, while new Generic 0/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).


Example: Lists

In the Java Collections Framework, the class List represents an ordered collection of objects of type MyClass. Upper bounds are specified using extends: A List is a list of objects of some subclass of MyClass, i.e. any object in the list is guaranteed to be of type MyClass, so one can iterate over it using a variable of type MyClass public void doSomething(List list) However, it is not guaranteed that one can add any object of type MyClass to that list: public void doSomething(List list) The converse is true for lower bounds, which are specified using super: A List is a list of objects of some superclass of MyClass, i.e. the list is guaranteed to be able to contain any object of type MyClass, so one can add any object of type MyClass: public void doSomething(List list) However, it is not guaranteed that one can iterate over that list using a variable of type MyClass: public void doSomething(List list) In order to be able to do both add objects of type MyClass to the list and iterate over it using a variable of type MyClass, a List is needed, which is the only type of List that is both List and List. Java syntax(Generics) The mnemonics PECS (Producer Extends, Consumer Super) from the book Effective Java by
Joshua Bloch Joshua J. Bloch (born August 28, 1961) is an American software engineer and a technology author, formerly employed at Sun Microsystems and Google. He led the design and implementation of numerous Java platform features, including the Java Collec ...
gives an easy way to remember when to use wildcards (corresponding to Covariance and Contravariance) in Java.


See also

* Bounded quantification *
Covariance and contravariance (computer science) Many programming language type systems support subtyping. For instance, if the type is a subtype of , then an expression of type should be substitutable wherever an expression of type is used. Variance refers to how subtyping between more comp ...
* Generics in Java#Type wildcards section explains lower and upper wildcard bounds


References

* The Java Language Specification, Third Edition (Sun), http://java.sun.com/docs/books/jls/ * Java Tutorials, Lesson Generics http://download.oracle.com/javase/tutorial/java/generics/index.html * Capturing Wildcards, http://bayou.io/draft/Capturing_Wildcards.html * Typkompatibilität in Java http://public.beuth-hochschule.de/~solymosi/veroeff/typkompatibilitaet/Typkompatibilitaet.html#Joker (in German) {{DEFAULTSORT:Wildcard (Java) Java (programming language) Polymorphism (computer science) Articles with example Java code