generic wildcard

The question mark in a generic type, as in a list of some unknown subtype of Number, meaning some specific type that this code does not know.

In more detail

An extends wildcard lets a method read elements as the upper bound but not add to the collection, because the exact element type is unknown. A super wildcard lets a method add values of the lower bound but read them back only as Object. Wildcards make method parameters accept more caller types without losing compile-time safety.

Examples

static double total(List<? extends Number> values)
static void addDefaults(List<? super Integer> values)

Related terms

Taught in

References

Certification courses