Wildcards
A wildcard is the question mark in a generic type, as in a list of some unknown subtype of Number. It lets a method accept a family of related generic types instead of exactly one.
Detailed Explanation
Generics are invariant. A List<Integer> is not a List<Number>, so a method declared to take List<Number> will refuse a list of integers. That rule prevents real bugs, but it also makes plenty of harmless code impossible to write. Wildcards are the escape hatch, and they are precise about which direction of flexibility you are asking for.
There are three forms.
List<? extends Number> means "a list of some single unknown type that is Number or below". You can read Number out of it. You cannot put anything in, except null. The compiler does not know whether the real list is a List<Integer> or a List<Double>, so no value it can name is guaranteed to be safe to add.
List<? super Integer> means "a list of some single unknown type that is Integer or above". You can add Integer values, because whatever the real list holds, Integer fits. Reading gives you Object, since the actual element type could be anything up the chain.
List<?> is the unbounded form: a list of something. You can check its size, iterate it as Object, and clear it. You cannot add. It is the honest way to say "I do not care what is in here", and it is strictly better than the raw type List, which turns off type checking altogether.
The mnemonic is PECS: Producer Extends, Consumer Super. If the parameter produces values that your method reads, use extends. If it consumes values that your method writes, use super. If it does both, use the plain type parameter with no wildcard. The JDK follows this everywhere once you start looking. Collections.copy(List<? super T> dest, List<? extends T> src) is the textbook case, and Comparator<? super T> on List.sort is why a Comparator<Object> can sort a list of users.
When would a learner reach for this? Almost always in a method signature, and almost always on a parameter. Say you write a reporting function that totals any collection of numeric readings. Declaring it as List<Number> makes it useless to callers holding a List<Integer>. Declaring it as List<? extends Number> makes it work for every numeric list in the codebase, with no casts and no copying.
Two rules of thumb keep this from getting out of hand. Use wildcards on parameters, not on return types: a method returning List<? extends Number> forces every caller to deal with the wildcard too, and the awkwardness spreads. And if a type parameter appears exactly once in a method signature, a wildcard is usually clearer than declaring <T> for it.
The cost is readability, plus a specific inconvenience: inside a method holding a List<? extends T>, you cannot write to it at all, and workarounds involve a private helper method that captures the wildcard into a real type parameter. Wildcards also disappear entirely at runtime, since they are a compile-time device layered on top of erasure.
Code Examples
import java.util.ArrayList;
import java.util.List;
class Pecs {
// PRODUCER: the method only reads out of this list, so use ? extends.
// Now it accepts List<Integer>, List<Double>, List<Number> — all of them.
static double sumAll(List<? extends Number> readings) {
double total = 0;
for (Number reading : readings) { // reading out is always safe
total += reading.doubleValue();
}
// readings.add(1); // rejected: the real list might be List<Double>
return total;
}
// CONSUMER: the method only writes Integers in, so use ? super.
static void fillWithFirstTen(List<? super Integer> sink) {
for (int i = 1; i <= 10; i++) {
sink.add(i); // safe: whatever it holds, an Integer fits
}
// Integer first = sink.get(0); // rejected: get() only promises Object
}
public static void main(String[] args) {
System.out.println(sumAll(List.of(1, 2, 3))); // prints: 6.0
System.out.println(sumAll(List.of(1.5, 2.5))); // prints: 4.0
List<Number> sink = new ArrayList<>(); // Number is a supertype of Integer
fillWithFirstTen(sink);
System.out.println(sink.size()); // prints: 10
}
}import java.util.List;
class Unbounded {
// List<?> is "a list of something". Honest, and still fully type checked,
// unlike the raw type List which switches checking off.
static int countNonNull(List<?> items) {
int count = 0;
for (Object item : items) { // elements are only knowable as Object
if (item != null) count++;
}
return count;
}
// You cannot mutate through a wildcard directly. A private helper
// "captures" the unknown type into a real type parameter T.
static void swapFirstTwo(List<?> list) {
swapHelper(list);
}
private static <T> void swapHelper(List<T> list) {
T first = list.get(0);
list.set(0, list.get(1));
list.set(1, first);
}
}Common Gotchas
You cannot add to a `? extends` collection
not even a value of the bound type. `List<? extends Number>` rejects `add(1)` because the underlying list could be a `List<Double>`. Only `null` is accepted.
`List<?>` and raw `List` are not the same
the wildcard keeps every compile-time check and merely hides the element type; the raw type disables generic checking for the whole expression and invites `ClassCastException` later. Never reach for the raw type to silence a wildcard error.
Two wildcards are two different unknowns
`void copy(List<?> a, List<?> b)` cannot move an element from `a` to `b`, because the compiler treats each `?` as its own captured type. Declare `<T>` when the two must match.
Wildcards belong on parameters
returning `List<? extends Number>` pushes the restriction onto every caller. Return a concrete `List<Number>` or `List<T>` and keep the flexibility on the input side.