CKTIS / Java
Wildcard type arguments in Java
Learn what `?`, `? extends T`, and `? super T` let you read or write, and how Java SE 21 applies those rules to lists, arrays, class tokens, and reference queues.
Version: Java SE 21
Syntax
Generic type arguments: G<?>, G<? extends T>, G<? super T> (T must already be in scope)
static <T> void copyFirst(List<? extends T> source, List<? super T> target)
List<? extends Employee> employees; List<? super Manager> destination;
Class<?> token = String.class;
List<?> list = List.of("tag"); List<?>[] lists = new List<?>[] { list };
Reference<? extends Asset> reference = queue.poll(); // queue has type ReferenceQueue<Asset>
Inside a class: @Override public String toString() { return "asset"; }In a type argument, ? stands for an unknown type that satisfies the generic declaration's bounds. It is not the conditional operator ?:. A List<?> is a list with one unknown element type, which the compiler can capture as a fresh type variable. You can read an element as Object, but you cannot simply add a non-null value. List<Object> has the specific type argument Object and accepts arbitrary objects if it supports insertion. Raw List bypasses some generic type checks.
Lists are invariant in their type argument: List<Manager> is not a subtype of List<Employee>, even when Manager extends Employee. List<? extends Employee> accepts a list whose element type is Employee or a subtype. You can read an Employee, but you cannot simply add even a Manager. List<? super Manager> accepts a list whose element type is Manager or a supertype. You can add a Manager or a subtype, but a read is guaranteed only as Object without a cast. This is PECS: producer extends, consumer super. Adding null passes the type check for each wildcard form, but the actual list may reject null or not support insertion. A wildcard does not make a list immutable.
In copyFirst, the declared type parameter <T> connects the two lists: ? extends T lets the method read a value as T, and ? super T lets it write that value. Neither wildcard declares T. When you need to reuse the same unknown element type from a List<?>, the compiler can infer the captured type for a generic helper that takes a List<T>.
List<?> and List<?>[] describe different things: one list versus an array whose slots may hold lists with different element types. Both types are reifiable, so new List<?>[2] is legal but new List<String>[2] is a compile-time error. The array store check does not check a list's type argument. Class<?> is a class token whose represented type is not specified in the signature. typeName(Class<?> type) can accept String.class and call getName() without knowing that type argument.
On a ReferenceQueue<Asset>, poll() does not wait: it removes and returns a Reference<? extends Asset>, or returns null immediately when the queue is empty. ? extends Asset bounds the referent's type, not the reference object's type. A non-null result of get() can be used as an Asset; a cleared reference yields null. The Object.toString override in the custom Asset class is the instance method public String toString(). @Override makes the compiler check that the method really overrides; it still runs when called through a variable typed as Object.
Examples
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
public class Main {
static class Asset {
@Override
public String toString() {
return "asset";
}
}
static class Employee extends Asset {}
static final class Manager extends Employee {}
static <T> void copyFirst(List<? extends T> source, List<? super T> target) {
if (!source.isEmpty()) {
target.add(source.get(0));
}
}
static String typeName(Class<?> type) {
return type.getName();
}
public static void main(String[] args) {
Manager manager = new Manager();
List<Manager> managers = List.of(manager);
List<? extends Employee> employees = managers;
Object asset = employees.get(0);
System.out.println(asset.toString());
List<? super Manager> destination = new ArrayList<Employee>();
copyFirst(managers, destination);
System.out.println("copied: " + destination.size());
List<?> tags = List.of("tag");
List<?>[] groups = new List<?>[] { destination, tags };
Object tag = groups[1].get(0);
System.out.println(tag);
System.out.println(typeName(String.class));
ReferenceQueue<Asset> queue = new ReferenceQueue<>();
System.out.println("empty: " + (queue.poll() == null));
WeakReference<Manager> reference = new WeakReference<>(manager, queue);
reference.enqueue();
Reference.reachabilityFence(manager);
Reference<? extends Asset> polled = queue.poll();
System.out.println("same cleared reference: " + (polled == reference && polled.get() == null));
}
}
Result
asset copied: 1 tag java.lang.String empty: true same cleared reference: true
`copyFirst` adds one element to the destination without removing it from the source. `enqueue()` explicitly clears and queues the weak reference, and `reachabilityFence` keeps `manager` strongly reachable until then. The output does not depend on garbage-collection timing.
Related lessons
- Java · Lesson 45 · Advanced · Version: 2
- Java · Lesson 58 · Basic · Version: 2
- Java · Lesson 59 · Basic · Version: 2
- Java · Lesson 33 · Intermediate · Version: 2
- Java · Lesson 34 · Intermediate · Version: 2
- Java · Lesson 36 · Intermediate · Version: 2
- Java · Lesson 37 · Intermediate · Version: 2
Sources
- Java Language Specification, Java SE 21, §4.5.1: Type Arguments of Parameterized Types
- Java Language Specification, Java SE 21, §4.7: Reifiable Types
- Java Language Specification, Java SE 21, §5.1.10: Capture Conversion
- Java Language Specification, Java SE 21, §15.10.1: Array Creation Expressions
- Java SE 21 API: List
- Java SE 21 API: Class and getName()
- Java SE 21 API: ReferenceQueue.poll()
- Java SE 21 API: Reference
- Java SE 21 API: WeakReference
- Java SE 21 API: Object.toString()
- Java SE 21 API: Override