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
- PECS — A rule of thumb for generic wildcards: a collection that produces values for you uses extends, and one that consumes values from you uses super.
- bounded type parameter — A type parameter restricted to types that support a capability, such as T extends Comparable<T>, so the code can call that capability.
- generics — Types that take other types as parameters, so one definition works for many kinds of value.
Taught in
- Generics Fundamentals — Modern Java: Virtual Threads & High-Throughput Services
- Generics And Collections — Modern Java: Virtual Threads & High-Throughput Services
- Arrays: Declare, Sort, Search, Compare — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Generics, Bounds And Wildcards — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep