Lesson · Generics
Bounds and wildcards: any list of numbers
18 min readBounded Types & Wildcards
A method typed List<Number> rejects a List<Integer> — Integer is a Number, but the list is not the list. Bounds (<T extends …>) and wildcards (? extends …) are how you say 'any list of numbers' without lying about what the method does.
The method that rejects its perfect argument
This is the most common generics surprise. Integer is a Number — and yet:
static double sum(List<Number> numbers) { ... } // exactly a List<Number> List<Integer> ints = List.of(1, 2, 3); sum(ints); // error: incompatible types: List<Integer> cannot be converted to List<Number>
Predict: why won't List<Integer> fit where List<Number> is expected? Because the relationship between Integer and Number does not travel into the list — List<Integer> and List<Number> are unrelated types. The fix is a question mark: ? extends.
Two kinds of blanks: named and anonymous
Generics give you two ways to loosen a type, and they answer different questions:
| Form | Plain meaning | Use it when |
|---|---|---|
List<Number> | exactly a list of Number — nothing else | you both read and write Number values |
List<? extends Number> | any list whose element type is Number or a subtype (Integer, Double, …) | you only take numbers out of it |
List<?> | any list at all; elements can only be looked at as Object | you only look at, count or print the items |
<T extends Comparable<T>> T max(...) | a named blank that must be comparable to its own type — the name T is then used again in the return type and body | the type must appear more than once in the method |
Named vs anonymous. <T extends Number> is a blank you name because you mention it twice; ? extends Number is the same meaning for a parameter you never name again.
Bounds board · which lists does each shape accept?
pick a parameter shapeCallers try to pass…
List<Integer> is not a List<Number> — the type argument must match exactly.
List<Double> is not a List<Number> either.
Exact match.
String is not a Number at all.
Inside the method, with List<Number>
Integer is a Number — List<Integer> is not a List<Number>. The wildcard accepts the subtype list, and pays for it with the right to add.
Reading through ? extends — and why writing stops
sum only takes values out, and the wildcard records exactly that:
static double sum(List<? extends Number> numbers) { double total = 0; for (Number n : numbers) { // read: every element is a Number total += n.doubleValue(); } return total; } // numbers.add(1); -> error: incompatible types: int cannot be converted to CAP#1
- Read: yes. Every element is guaranteed to be at least a Number, so
n.doubleValue()compiles. - Write: no.
numbers.add(1)is refused: the compiler only knows the element type is *some* unknown subtype of Number, and it will not guess which one. - The refusal names the unknown:
CAP#1is the compiler's label for the type it captured out of? extends Number. - This is the promise working, not a limitation: 'I only take values out' — and the compiler holds you to it.
The wildcard is a statement about traffic. ? extends says values only leave; ? super (next lesson) says values only arrive; plain T says both.
The bound unlocks the methods
A plain <T> can only do Object things — equals, toString. Everything else must be promised by a bound:
static <T> T max(List<T> values) { T best = values.get(0); for (T value : values) { if (value.compareTo(best) > 0) { // error: cannot find symbol: method compareTo(T) best = value; } } return best; }
- Without the bound the compiler only knows T is some object:
compareTodoes not exist on that vocabulary. - With `<T extends Comparable<T>>` the promise is made: T can compare itself with another T — which is exactly what
maxneeds. - Comparable<T> is comparable to its own type — the recursive shape (
T extends Comparable<T>) is the standard pattern for max, sort and merge. - More than one promise:
&stacks bounds —<T extends Number & Comparable<T>>means T is both.
Name what it is: a bound is a promise attached to the blank, and every promise hands your method a new method to call: Number gives doubleValue(), Comparable gives compareTo().
The unbounded ? for lookers-on
? with no bound means 'any list', and the body can only treat elements as Objects:
static void printAll(List<?> items) { for (Object item : items) { // Object is all the compiler promises System.out.println(item); } }
printAll(List<?>)accepts a list of anything: strings, numbers, even mixed raw content.- Inside, each item is an
Object: print it, count it, put it in a box — but no String or Number methods. - Use it when the method genuinely does not care what the items are.
The bounds playbook
Choosing the parameter shape, in order:
- Start with the exact type —
List<Number>— and loosen only when a real caller needs it. - Does the method only take values out? Add
? extendsto the element type. - Does it need to name the type again (return it, compare it, use it twice)? Use a named blank with a bound:
<T extends …>. - Does it just look at items as objects (print, count)?
List<?>. - The bound decides your vocabulary: extend Number to call
doubleValue(), extend Comparable to callcompareTo().
Bounds describe what the method does, not what callers must do. extends says 'I only take things out' — so the compiler protects you from putting things in.
List<Integer> is not a List<Number> — ? extends bridges the two
A parameter typed List<Number> accepts exactly that list type. A parameter typed List<? extends Number> accepts any list of numbers: read as Number, never written. A named blank with a bound (<T extends Comparable<T>>) is for methods that need the type itself in more than one place, and the bound is what unlocks its methods. Generics are invariant: subtyping never travels into a type argument.
Interview angles
Why doesn't List<Integer> fit into a parameter typed List<Number>?+
Generics are invariant: List<Integer> and List<Number> are unrelated types, even though Integer extends Number.
The invariance is protecting you. If List<Integer> were a List<Number>, the method could add 3.14 into your list of integers — the type system would have lied. Since the method may write, Java keeps the types apart; when your method does not write, the wildcard reopens the door safely.
Common misconception: That 'Integer extends Number' should make 'List<Integer> extends List<Number>' — it does not, for any type argument.
Follow-up: What signature change makes sum accept List<Integer>?
What can you do with List<? extends Number> inside the method?+
Read: every element can be treated as Number. Write: adding is refused — the compiler does not know which subtype the actual list holds.
The refusal says int cannot be converted to CAP#1: CAP#1 is the compiler's name for the unknown type captured from the wildcard. It is not being difficult — the real list might be List<Double>, and adding an Integer would corrupt it.
Common misconception: That extends makes the list read-only because lists are somehow immutable — the list is as mutable as ever; the static type is what refuses the call.
Follow-up: So how does a method that must write accept several list types?
What does `<T extends Comparable<T>>` say in plain English?+
T is any type that can compare itself with another T. The bound is what makes value.compareTo(other) callable.
Without it, the compiler only knows T is some object and reports cannot find symbol: method compareTo(T). The self-reference (T comparable to T) is what max needs: comparing two elements of the same list.
Common misconception: That the bound changes what callers can pass — it changes what the method can do; callers just pick any comparable type.
Follow-up: What does the compiler say when you remove the bound?
What is List<?> and when is it the right parameter?+
Any list at all. Elements can only be read as Object, and adding is refused. It fits methods that only look at items: print, count, contains.
It is not the same as a raw List: a raw List turns every check off with a warning, while List<?> keeps the checks — you simply are not allowed to add anything or assume any element type.
Common misconception: That List<?> and List<Object> are the same — List<Object> only accepts lists declared with Object, while List<?> happily accepts a List<String> too.
Follow-up: Can you pass List<String> where List<?> is expected?
When do you reach for a named blank instead of a wildcard?+
When the type must be used in more than one place: parameter and return type, or two parameters that must match each other.
max(List<T>): T appears in the parameter and the return type, so it must have a name. sum(List<? extends Number>): the type is never mentioned again — double is returned — so a wildcard is enough and accepts more callers.
Common misconception: That named blanks are 'stronger' and wildcards are shortcuts — they express different promises: one names the type, the other hides it.
Follow-up: What if the method needs two promises on the same type?