Bounded vs Unbounded Wildcards in Java — ?, ? extends T, ? super T with Real Examples
Fast recall
Quick revision
Core answer
Object and can't add anything except null. `? extends T` (upper-bounded) = "T or any subtype" — safe to read as T, can't add (compiler doesn't know the exact subtype). `? super T` (lower-bounded) = "T or any supertype" — safe to add T or any subtype of T, reads come back as Object. When to use: ? extends for reading (producer), ? super for writing (consumer), ? when you don't care about the type at all (e.g., counting elements). The mnemonic is PECS — Producer-Extends, Consumer-Super.Whether the candidate knows the read-only and write-only nature of each wildcard form and can apply PECS to real API design
Confusing `? extends T` as writable (it isn't — except for null). Thinking `? super T` reads as T (it reads as Object). Using raw `List` instead of `List<?>`
Explain why `? extends` is read-only via the compiler's capture mechanism. Use `Collections.copy(List<? super T> dest, List<? extends T> src)` as the canonical PECS example and explain why both bounds are needed
Interview-ready guide
Interview answer
Wildcards exist because Java generics are invariant — List is not a List. That's safe but inflexible, and wildcards restore controlled flexibility.
Three wildcard forms
- `?` — some unknown type; read-only as
Object; use when the type doesn't matter (e.g.,printAll(List>),size()) - `? extends T` — T or any subtype; safe to read as T; can't add (compiler doesn't know the exact subtype)
- `? super T` — T or any supertype; safe to add T and its subtypes; reads come back as
Object
The mnemonic: PECS
- Producer-Extends — if you're reading from the collection, use
? extends T - Consumer-Super — if you're writing into the collection, use
? super T
Why the asymmetry
? extends T: the list might beListorList— adding anything risks mismatching the actual type? super T: the list is guaranteed to accept T and its subtypes, so adding is safe; but reads can only guaranteeObject
Canonical example — `Collections.copy`
copy(List super T> dest, List extends T> src)— source is a producer, destination is a consumer- Lets you copy from
ListintoListwithout losing type safety
When to use named type parameters instead
- Two parameters must share the same type:
void swap(List , int, int) - Return type depends on an argument
- You need to reference the type inside the method
Recommendation: For API inputs, prefer wildcards — they maximize caller flexibility. ? extends T when you only read, ? super T when you only write, ? when you don't care. Reserve named type parameters for when you must relate multiple parameters or a return value to the same type. And never use raw List when List> is available — raw types disable all generic type checking.
Bounded vs Unbounded Wildcards in Java — `?`, `? extends T`, `? super T` with Real Examples
Direct Answer
? (unbounded) = "some unknown type" — you can read as Object and can't add anything except null. ? extends T (upper-bounded) = "T or any subtype" — safe to read as T, can't add (compiler doesn't know the exact subtype). ? super T (lower-bounded) = "T or any supertype" — safe to add T or any subtype of T, reads come back as Object. When to use: ? extends for reading (producer), ? super for writing (consumer), ? when you don't care about the type at all (e.g., counting elements). The mnemonic is PECS — Producer-Extends, Consumer-Super.
Common Mistakes & Pitfalls
Confusing ? extends T as writable (it isn't — except for null). Thinking ? super T reads as T (it reads as Object). Using raw List instead of List<?>
Check your understanding
Answer these without looking back at the article.
- Why can you add `null` to a `List<? extends Number>` but not an `Integer`?
- What does "capture conversion" mean, and when does the compiler apply it to wildcards?
- Why is `Collections.copy(dest, src)` declared with both `? super T` and `? extends T`?
- How do bounded wildcards interact with method overloading and erasure?
- Should a method return `List<?>` or `List<T>`? What do developers generally prefer?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.