01 / 21
? < >

Generic
Wildcards in Java

Master the <?> and <? extends T> syntax to write flexible, type-safe methods that work with families of generic types.

AP Computer Science Java Generics

What Are Generics?

Generics let you write classes and methods that work with any type while keeping full compile-time type safety. Instead of casting from Object, you declare a type parameter like <T>.

// Without generics: dangerous cast List list = new ArrayList(); list.add("hello"); String s = (String) list.get(0); // With generics: type-safe List<String> list = new ArrayList<>(); list.add("hello"); String s = list.get(0); // no cast needed

The Problem Wildcards Solve

You might expect a List<Dog> to be assignable to a List<Animal> because Dog extends Animal. But in Java, generics are invariant: they do not follow the same inheritance rules as their type arguments.

Animal Dog Cat extends extends List<Dog> List<Animal> ✕

Introducing <?>

The wildcard symbol ? stands for "some unknown type." It lets you declare a reference that can point to a generic container of any type argument.

<T> Type Parameter
Declares a name you use repeatedly inside a class or method. You define it.
<?> Wildcard
Says "I don't care which type" when accepting an already-parameterized type as an argument.

The Three Wildcard Forms

<?>
Unbounded: accepts any type. Useful when you only need methods from Object.
<? extends T>
Upper-bounded: accepts T or any subtype of T. Great for reading.
<? super T>
Lower-bounded: accepts T or any supertype of T. Great for writing.

Unbounded: <?>

public static void printAll(List<?> items) { for (Object item : items) { System.out.println(item); } }
1
<?> means "List of something, I don't know what."
2
You can read elements as Object (every class extends Object).
3
You cannot add to it (except null) because the compiler cannot guarantee type safety.

Upper-Bounded: <? extends T>

public static double sum(List<? extends Number> nums) { double total = 0; for (Number n : nums) { total += n.doubleValue(); } return total; }
Number Integer Double BigDecimal All accepted by <? extends Number> ✓ ✓ ✓

<T> vs <?>: Key Distinction

<T> Type Parameter <?> Wildcard
Where used Class / method declaration Method parameter type
Naming Has a name (T, E, K, V) Anonymous (just ?)
Reuse in body Yes: T item = list.get(0) No: unknown type
Adding elements Allowed (type is known) Restricted (type unknown)
Typical goal Define reusable structures Accept flexible inputs

How to Choose the Right Wildcard

1
Identify the action: Does your method only read from the collection, or does it also write?
2
Read-only, any type: Use <?>. You will receive elements as Object.
3
Read-only, specific family: Use <? extends T>. You will receive elements as T.
4
Need to add elements: Consider <? super T> (lower-bounded) or a named type parameter <T>.
5
Need to reference the type: Use a named <T> instead of a wildcard.

Example 1: Counting Elements

Write a method that returns the number of null elements in any list, regardless of its type argument.

public static int countNulls(List<?> list) { int count = 0; for (Object item : list) { if (item == null) count++; } return count; } // Works with ANY list countNulls(new ArrayList<String>()); // ✓ countNulls(new ArrayList<Integer>()); // ✓ countNulls(new ArrayList<Dog>()); // ✓

Example 2: Summing Numbers

Write a method that sums any list of numeric types: Integer, Double, Long, etc.

public static double sumList(List<? extends Number> nums) { double total = 0.0; for (Number n : nums) { total += n.doubleValue(); } return total; } List<Integer> ints = List.of(1, 2, 3); List<Double> dbls = List.of(1.5, 2.5); sumList(ints); // 6.0 ✓ sumList(dbls); // 4.0 ✓

Example 3: Finding the Max

A method that finds the maximum element from a list of any Comparable subtype:

public static <T extends Comparable<? super T>> T findMax(List<? extends T> list) { T max = list.get(0); for (T item : list) { if (item.compareTo(max) > 0) { max = item; } } return max; } findMax(List.of(3, 7, 1)); // 7 findMax(List.of("a", "z", "m")); // "z"

Read vs Write with Wildcards

✓ Reading (GET) Object item = list.get(0); for (Object o : list) { } list.size(); list.contains(obj); All safe with <?> ✕ Writing (ADD) list.add("hello"); list.set(0, value); Compile error! (Exception: list.add(null) is always allowed)

Wildcard Subtyping Hierarchy

List<?> List<? extends Number> List<Integer> List<Double> List<Long> Arrows point from subtype to supertype

Common Mistakes

✕ Adding to <?>
Trying to call list.add(x) on a List<?> or List<? extends T>. The compiler will refuse because it cannot verify the type.
✕ Confusing <?> with <Object>
List<?> accepts any list. List<Object> only accepts a list specifically typed as Object, not List<String>.
✕ Using ? in declarations
Writing class Box<?> is illegal. Wildcards go in method parameters, not class definitions. Use <T> when declaring.

Wildcard Cheat Sheet

Syntax Accepts Read As Can Add?
<?> Any type Object No
<? extends T> T or subtypes T No
<? super T> T or supertypes Object Yes (T)
<T> Exact type T Yes (T)

Question 1 of 5

Which of the following method signatures allows you to pass a List<String>, List<Integer>, or List<Dog> as an argument?

Question 2 of 5

Given a variable declared as List<? extends Number> nums, which operation is allowed?

Question 3 of 5

Why does List<Dog> NOT count as a subtype of List<Animal> in Java?

Question 4 of 5

Which statement about the unbounded wildcard <?> is false?

Question 5 of 5

You need a method that accepts a List<Integer>, List<Double>, or List<Long> and returns their sum. Which parameter type is most appropriate?