Understanding Java Generic Wildcards
Java's wildcard character ? serves as a special type parameter representing an unknown type. Wildcards enhance code flexibility when working with generic types, allowing generic containers to reference various generic objects. There are three primary wildcard usage scenarios: unbounded wildcards, upper-bounded wildcards, and lower-bounded wildcards.
1. Unbounded Wildcard (?)
The unbounded wildcard ? denotes any type, primarily used when reading or manipulating collections without concern for the specific element type.
public void displayItems(List<?> items) {
for (Object element : items) {
System.out.println(element);
}
}
Here, List<?> represents a list of unknown type elements, enabling the displayItems method to accept any List instance.
2. Bounded Wildcards
Bounded wildcards restrict the unknown type's range, including upper bounds (? extends Type) and lower bounds (? super Type).
Upper-Bounded Wildcard (? extends Type)
The upper-bounded wildcard ? extends Type specifies that any type extending Type is acceptable.
public void showNumbers(List<? extends Number> numbers) {
for (Number num : numbers) {
System.out.println(num);
}
}
List<? extends Number> accepts List<Integer>, List<Double>, or any other Number subclass list.
Lower-Bounded Wildcard (? super Type)
The lower-bounded wildcard ? super Type permits lists to reference Type or any of its supertypes.
public void insertValue(List<? super Integer> container) {
container.add(42); // Accepts Integer or its supertypes
}
List<? super Integer> could be List<Integer>, List<Number>, or List<Object>.
Wildcard Usage Guidelines
- Use
? extends Twhen readingTelements from a collection without writing (producer). - Use
? super Twhen writtingTelements to a collection without reading (consumer). - Use the unbounded wildcard
?when neither reading nor writing is required.