Bounds and wildcards
What you will learn
A bound restricts which types may fill a type parameter. A wildcard instead describes a family of parameterized types at a use site. Together they let you express which values a method can safely read or write.
- Use upper bounds and wildcard bounds for safe access.
- Write a copy method using producer extends and consumer super.
Use a fresh directory per example. Save as Main.java, compile with javac --release 21 Main.java, then run java Main. Only JDK standard-library dependencies are needed. List, Map and Set operations used here are brief previews of later collection chapters.
Use a numeric upper bound
import java.util.*;public class Main { static <T extends Number> double twice(T value) { return 2 * value.doubleValue(); } static double sum(List<? extends Number> values) { double result = 0; for (Number n : values) result += n.doubleValue(); return result; } public static void main(String[] args) { System.out.println(twice(3)); System.out.println(twice(1.25)); System.out.println(sum(List.of(2, 3))); }}Expected output:
6.02.55.0The bound permits doubleValue. The wildcard permits an integer list; the loop reads numbers. Double arithmetic is approximate in general.
Rules and mental model
In <T extends Number>, the compiler can use Number members on T. Multiple bounds are joined with &, with a class bound first if present. List<? extends Number> can be a list of Integer, Double or another Number subtype; reading yields Number, but inserting a non-null Number is not generally safe because the exact element type is unknown. List<? super Integer> can accept Integer values; its elements can only be read as Object without further checks. PECS is a useful guideline: producer extends, consumer super. A producer supplies values to your method; a consumer receives them. A list used for both reads and writes of the same type may need List<T>. Wildcards do not change whether a list is modifiable. The example uses separate mutable destination lists and a fixed source to avoid self-copy problems.
Move data from a producer into a consumer
import java.util.*;public class Main { static <T> void copy(List<? extends T> source, List<? super T> destination) { for (T value : source) destination.add(value); } public static void main(String[] args) { List<Integer> source = List.of(2, 4); List<Number> target = new ArrayList<>(); copy(source, target); System.out.println(target); List<Object> other = new ArrayList<>(); copy(List.of("Map"), other); System.out.println(other); }}Expected output:
[2, 4][Map]The source provides values assignable to T. The destination accepts T even when its declared element type is broader. This method appends; unlike Collections.copy, it does not replace existing slots.
Common mistakes
List<Integer> is not a subtype of List<Number>. If it were, a method accepting List<Number> could insert a Double into an integer list. List<? extends Number> is not an immutable list; operations such as clear may still be available.
Practice
Write addDefaults(List<? super Integer>) that appends 1 and 2. Call it with a List<Number> containing 0.5 and with an empty List<Object>. Print both lists.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Bounds and wildcards — knowledge check
Apply the chapter contracts and reason about Java 21-compatible examples.
What can be read from List<? extends Number>?
Which list can consume an Integer under List<? super Integer>?
Does an extends wildcard make a list immutable?