Generics: wildcards and when to use them
Why ? extends and ? super exist, what PECS means, and when a wildcard beats a type parameter.
Adetayo Akinsanya2026-09-295 min read
- Java
- Core Java
- Generics
- Wildcards
- PECS

Part 18 of the series.Read Generics: writing code that works for more than one type (Part 17) first if you have not.
In this article (7 sections)
public static double sumList(List<Number> numbers) {
double total = 0;
for (Number n : numbers) {
total += n.doubleValue();
}
return total;
}
This compiles. But when you try to pass a List<Integer>:
List<Integer> ints = List.of(1, 2, 3);
System.out.println(sumList(ints)); // compile error
The error: “incompatible types: List<Integer> cannot be converted to List<Number>”.
Integer is a subtype of Number. So why does List<Integer> not count as a List<Number>?
Why generic types are not covariant
Consider what would happen if List<Integer> were assignable to List<Number>:
List<Integer> ints = new ArrayList<>();
List<Number> numbers = ints; // suppose this compiled
numbers.add(3.14); // adds a Double into the Integer list
Integer n = ints.get(0); // ClassCastException
Adding a Double to a List<Number> would put a non-Integer into what ints sees as a List<Integer>. The type system would be unsound. Java prevents this by making generic types invariant: List<Integer> is not a subtype of List<Number>, even though Integer is a subtype of Number.
Arrays are covariant in Java, which is why this compiles and throws at runtime:
Integer[] ints = {1, 2};
Number[] numbers = ints; // allowed (arrays are covariant)
numbers[0] = 3.14; // ArrayStoreException at runtime
Generics fix this by moving the check to compile time.
Upper bounded wildcards: ? extends T
To accept a list of Number or any subtype of Number, use an upper bounded wildcard:
public static double sumList(List<? extends Number> numbers) {
double total = 0;
for (Number n : numbers) {
total += n.doubleValue();
}
return total;
}
Now sumList(ints) compiles. List<? extends Number> matches List<Integer>, List<Double>, List<Float>, and List<Number> itself.
The tradeoff: you can read from a List<? extends Number> safely (each element is at least a Number), but you cannot write to it. The compiler rejects numbers.add(42) because it cannot guarantee the actual list type. If numbers is a List<Integer>, adding a Double would corrupt it. The wildcard makes the list read-only from the compiler’s perspective.
Lower bounded wildcards: ? super T
The opposite situation: a list you want to write Integer values into, without caring whether it was declared as List<Integer>, List<Number>, or List<Object>:
public static void fillWithZero(List<? super Integer> list, int count) {
for (int i = 0; i < count; i++) {
list.add(0);
}
}
List<? super Integer> matches any list whose element type is Integer or a supertype of Integer. Adding 0 (an Integer) to any such list is safe: a List<Number> accepts Integer, a List<Object> accepts Integer.
The tradeoff: you can write Integer values into a List<? super Integer>, but reading out a specific type is not safe. list.get(0) returns Object, not Integer, because the list might contain elements added elsewhere of a type other than Integer.
PECS: producer extends, consumer super
The rule that follows from the two wildcards:
If a parameter only produces values you read from it, use ? extends T. The list gives you values; it is the producer.
If a parameter only consumes values you write into it, use ? super T. The list accepts values; it is the consumer.
If a parameter does both, use a plain type parameter.
// source produces; dest consumes
public static <T> void copy(List<? extends T> source, List<? super T> dest) {
for (T item : source) {
dest.add(item);
}
}
This signature accepts copy(List<Integer> source, List<Number> dest) even though Integer and Number are different types, because the bounds make the variance explicit.
The name “PECS” comes from Joshua Bloch’s Effective Java. It is a mnemonic for the direction of data flow, not a law separate from the type rules above.
Unbounded wildcards: ?
List<?> matches any parameterized list regardless of element type. It is the most permissive wildcard.
public static void printSize(List<?> list) {
System.out.println(list.size());
}
You can call methods that do not depend on the element type (like size(), isEmpty(), clear()). You cannot add elements (except null, which the compiler allows but that is usually wrong). Reading returns Object.
Use List<?> when your code genuinely does not care about the element type. If your code uses the elements as a specific type, use a bounded wildcard or a type parameter.
Wildcard versus type parameter
Both <T> void process(List<T> list) and void process(List<?> list) express “a list of some element type.” The difference is whether the element type needs a name.
If you use the type in more than one place in the signature, or in the return type, use a type parameter:
// T appears twice: input list and return type
public static <T> T first(List<T> list) {
return list.get(0);
}
If the element type appears only once and you never need to refer to it, a wildcard is simpler:
public static void printAll(List<?> list) {
for (Object o : list) {
System.out.println(o);
}
}
Using a type parameter <T> where you never use T by name adds a meaningless constraint. Wildcards are more precise in that case.
Prediction exercise
public static <T extends Comparable<T>> void sort(List<T> list) {
// bubble sort for illustration
for (int i = 0; i < list.size() - 1; i++) {
for (int j = 0; j < list.size() - 1 - i; j++) {
if (list.get(j).compareTo(list.get(j + 1)) > 0) {
T temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
}
Can you call sort with a List<Integer>? With a List<Object>? With a List<String>? For the case that fails, explain which bound is violated and what signature would fix it. The next article covers what happens to type parameters after compilation: erasure, the bridge methods the compiler generates, and what raw types are. Generics: erasure and raw types continues from here.
Questions or corrections
Found a mistake or have a question about this breakdown? Reach out directly.