Course contentsShow
← Java
Lesson 954 of 2,87022. Generics: Bounds and WildcardsFree lesson

Introduction to Capture Conversion

How the compiler infers a concrete type parameter when calling generic methods with wildcard arguments.

Introduction to Capture Conversion

What you'll learn: How the compiler secretly creates a concrete type to make wildcard-based generic method calls type-safe.


When you pass a collection with a wildcard type (like List<?> or List<? extends Number>) to a generic method, the compiler faces a puzzle: the method expects a concrete type parameter like T, but you've given it a wildcard that represents "some unknown type."

The compiler solves this through capture conversion — it invents a fresh, invisible type parameter to represent "whatever specific type this wildcard actually is." This captured type exists only within the method call and ensures type safety.

Why This Matters

Consider calling a generic helper method:

public static <T> void processFirst(List<T> list) {
    if (!list.isEmpty()) {
        T item = list.get(0);
        list.add(item); // Safe: we know item is of type T
    }
}

If you pass List<?> to this method, the compiler performs capture conversion. It thinks: "This list contains some specific type — I'll call it CAP#1." Now T becomes CAP#1, and the method body type-checks correctly because adding and retrieving both use the same captured type.

The Invisible Helper

Think of capture conversion like the compiler saying: "I don't know what type is in that box, but I'll write it down on a sticky note and make sure you only mix things that match that note." The actual type stays unknown, but consistency is enforced.

Without capture conversion, you couldn't safely call generic methods with wildcard arguments — the type system would break down.

Key Takeaway: Capture conversion lets the compiler infer a concrete (though anonymous) type parameter when generic methods receive wildcard arguments, preserving type safety behind the scenes.