Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Java Lower Bounded Wildcards: When to Use ? super T

Learn how Java lower bounded wildcards (? super T) work, when to use them, and how they differ from upper bounds in generic methods.

~5 min readJava
#java generics#wildcards#pecs#type safety
Diagram showing a Java list with a lower bounded wildcard accepting Integer and its supertypes Number and Object.

In Java generics, a lower bounded wildcard (? super T) lets a method accept a collection whose element type is T or a supertype of T. It is most useful for write-only operations, where the method puts values into a collection and does not need to read them back with a more specific type.

The Problem Lower Bounded Wildcards Solve

When you declare a generic method parameter, the type must match exactly unless you introduce a wildcard. Consider a method that adds an element to a list:

java
public static void addNumber(List<Integer> numbers, Integer value) { numbers.add(value); }

This works only for List<Integer>. If you have a List<Number> or a List<Object>, you cannot pass it, even though adding an Integer to either is perfectly safe. The compiler rejects the call because List<Number> is not a subtype of List<Integer>.

A lower bounded wildcard, written as ? super T, is the Java mechanism for this situation. It changes the method signature so the parameter accepts any list whose element type is Integer or a supertype of Integer:

java
public static void addNumber(List<? super Integer> numbers, Integer value) { numbers.add(value); }

Now the method accepts List<Integer>, List<Number>, and List<Object>. The wildcard ? super Integer means some unknown type that is Integer or a supertype of Integer. That unknown type is what makes the parameter flexible.

Syntax and Placement

The syntax is straightforward:

java
List<? super Integer>

The wildcard ? is followed by the super keyword and a type. The type can be a class, an interface, or another type parameter. For example:

java
List<? super Integer> list = new ArrayList<Number>(); List<? super Integer> list2 = new ArrayList<Object>();

You cannot assign a List<String> to List<? super Integer> because String is neither Integer nor a supertype of Integer. The compiler enforces this at compile time.

Lower bounded wildcards can appear in method parameters, field declarations, and local variable declarations. They are not useful in return types: if a method returns List<? super Integer>, the caller cannot safely read anything except Object from it, which defeats the purpose of returning a typed collection.

What You Can and Cannot Do

The restriction on reading is the key tradeoff. Given:

java
List<? super Integer> list = new ArrayList<Number>();

You can add an Integer:

java
list.add(42);

You can also add values of the bound type or any subtype of the bound. For ? super Number, that includes Number, Integer, Double, and Long. For ? super Integer, Integer is the only practical choice because Integer is final and has no subclasses. You cannot add a Number value that is not an Integer, such as a Double, to a List<? super Integer>, because the list's actual type parameter is unknown. It might be List<Integer>, in which case adding a Double would violate type safety.

When reading, the compiler knows only that the element type is Object or some supertype of Integer. Since Object is the common supertype of everything, you can read elements only as Object:

java
Object first = list.get(0);

You cannot assign the result to Integer without a cast, and a cast is unsafe because the list might actually contain Number or Object instances.

This asymmetry, write access to subtypes of the bound and read access only to Object, is what makes lower bounded wildcards useful for consumers.

PECS: Producer Extends, Consumer Super

The PECS rule, popularized by Joshua Bloch in Effective Java, states:

  • Use ? extends T when the collection produces values, meaning you read from it.
  • Use ? super T when the collection consumes values, meaning you write to it.

A method that copies elements from one list to another demonstrates both sides:

java
public static <T> void copy(List<? super T> dest, List<? extends T> src) { for (int i = 0; i < src.size(); i++) { dest.add(src.get(i)); } }

The destination list only receives values, so it uses ? super T. The source list only provides values, so it uses ? extends T. This is the signature used by Collections.copy.

The same principle applies to a method that fills a list with a default value:

java
public static <T> void fill(List<? super T> list, T value) { for (int i = 0; i < list.size(); i++) { list.set(i, value); } }

Calling fill with a List<Object> and an Integer value works because Object is a supertype of Integer.

Practical Examples

The standard library uses lower bounded wildcards in several places. Collections.addAll is one:

java
public static <T> boolean addAll(Collection<? super T> c, T... elements)

This lets you add elements to a collection whose element type is a supertype of the element type:

java
List<Number> numbers = new ArrayList<>(); Collections.addAll(numbers, 1, 2.5, 3L);

All three arguments can be treated as Number, so the ? super Number bound accepts List<Number> and would also accept List<Object>.

Another example is TreeSet's constructor that accepts a comparator:

java
public TreeSet(Comparator<? super E> comparator)

A TreeSet<Integer> can accept a Comparator<Number> because Number is a supertype of Integer. This is useful when you have a comparator written against a common supertype and want to reuse it for multiple specific types.

Common Mistakes

A frequent mistake is using a lower bounded wildcard where an upper bound is needed, or vice versa. If a method only reads from a collection and returns a value, ? super T forces the caller to read as Object, which is usually wrong:

java
public static <T> T first(List<? super T> list) { return (T) list.get(0); // unsafe cast }

The cast is unsafe because the list might contain a supertype instance. The correct signature for reading is ? extends T:

java
public static <T> T first(List<? extends T> list) { return list.get(0); }

Another mistake is using a lower bounded wildcard in a return type. A method that returns List<? super Integer> forces every caller to treat the result as a list of unknown supertype, which removes useful type information. Return types should use concrete types or upper bounds.

A third mistake is trying to add a value outside the lower bound:

java
List<? super Integer> list = new ArrayList<Number>(); list.add(3.14); // compile error

The compiler rejects this because the actual type parameter might be Integer, and a Double is not an Integer. The wildcard only guarantees that subtypes of the bound can be added.

Type Erasure and Runtime Behavior

At runtime, generic type information on collection objects is erased. A List<? super Integer> and a List<Integer> are both erased to the raw List type, so the wildcard has no runtime representation and no performance cost. The compiler performs the safety checks before the code runs.

For type erasure, a wildcard is replaced by its upper bound. Since ? super Integer has no explicit upper bound, it erases to Object, the same as an unbounded wildcard. A particular list instance does not carry its declared type argument at runtime, so you cannot inspect a list object and discover whether it was declared with a lower bound. Generic method signatures may still be visible through reflection, because the compiler can store a Signature attribute; this is separate from the runtime type of the collection itself.

Wildcards themselves do not make a method generic. If you need the same type parameter to appear in a lower bound and in other arguments or a return type, declare a generic method. For example, copy uses <T> so the source and destination are connected by the same type. A non-generic method like addNumber(List<? super Integer>, Integer) is enough when you only need to write one fixed type.

When a Lower Bound Is the Wrong Choice

A lower bounded wildcard is not always the right tool. If you need to both read and write values of a specific type, a concrete type parameter is clearer:

java
public static <T> void process(List<T> list, T value) { list.add(value); T first = list.get(0); }

Here T gives full read and write access. A lower bound would force reads to Object and make the method harder to use.

If you only read, use ? extends T. If you only write, use ? super T. If you do both with the same type, use a type parameter. This decision rule covers most real-world cases and avoids the awkwardness of casting from Object.

Try matching browser tools

All tools

Small utilities that fit Java work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Java guides

More in Java

All Java guides

The guides filed next to this one in Java, in library order.