---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 17 Edition"
java_version: 17
chapter: "Chapter 5. Conversions and Contexts"
section: "5.1.10"
heading: "5.1.10. Capture Conversion"
parent: "5.1. Kinds of Conversion"
source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-5.html#jls-5.1.10
id: "5.1.10"
url: https://jvm.obfuscated.software/jls/se17/5.1.10.html
raw: https://jvm.obfuscated.software/jls/se17/5.1.10.md
---

# 5.1.10. Capture Conversion

Let G name a generic type declaration ([§8.1.2](8.1.2.md), [§9.1.2](9.1.2.md)) with *n* type parameters A_1,...,A_n with corresponding bounds U_1,...,U_n.

There exists a *capture conversion* from a parameterized type G`<`T_1,...,T_n`>` ([§4.5](4.5.md)) to a parameterized type G`<`S_1,...,S_n`>`, where, for 1 ≤ *i* ≤ *n* :

- If T_i is a wildcard type argument ([§4.5.1](4.5.1.md)) of the form `?`, then S_i is a fresh type variable whose upper bound is U_i`[A_1:=S_1,...,A_n:=S_n]` and whose lower bound is the null type ([§4.1](4.1.md)).

- If T_i is a wildcard type argument of the form `?` `extends` B_i, then S_i is a fresh type variable whose upper bound is glb(B_i, U_i`[A_1:=S_1,...,A_n:=S_n]`) and whose lower bound is the null type.

  glb(V_1,...,V_m) is defined as V_1 `&` ... `&` V_m.

  It is a compile-time error if, for any two classes (not interfaces) V_i and V_j, V_i is not a subclass of V_j or vice versa.

- If T_i is a wildcard type argument of the form `?` `super` B_i, then S_i is a fresh type variable whose upper bound is U_i`[A_1:=S_1,...,A_n:=S_n]` and whose lower bound is B_i.

- Otherwise, S_i = T_i.

Capture conversion on any type other than a parameterized type ([§4.5](4.5.md)) acts as an identity conversion ([§5.1.1](5.1.1.md)).

Capture conversion is not applied recursively.

Capture conversion never requires a special action at run time and therefore never throws an exception at run time.

Capture conversion is designed to make wildcards more useful. To understand the motivation, let's begin by looking at the method `java.util.Collections.reverse()`:

```
public static void reverse(List<?> list);
```

The method reverses the list provided as a parameter. It works for any type of list, and so the use of the wildcard type `List<?>` as the type of the formal parameter is entirely appropriate.

Now consider how one would implement `reverse()`:

```
public static void reverse(List<?> list) { rev(list); }
private static <T> void rev(List<T> list) {
    List<T> tmp = new ArrayList<T>(list);
    for (int i = 0; i < list.size(); i++) {
        list.set(i, tmp.get(list.size() - i - 1));
    }
}
```

The implementation needs to copy the list, extract elements from the copy, and insert them into the original. To do this in a type-safe manner, we need to give a name, `T`, to the element type of the incoming list. We do this in the private service method `rev()`. This requires us to pass the incoming argument list, of type `List<?>`, as an argument to `rev()`. In general, `List<?>` is a list of unknown type. It is not a subtype of `List<T>`, for any type T. Allowing such a subtype relation would be unsound. Given the method:

```
public static <T> void fill(List<T> l, T obj)
```

the following code would undermine the type system:

```
List<String> ls = new ArrayList<String>();
List<?> l = ls;
Collections.fill(l, new Object());  // not legal - but assume it was!
String s = ls.get(0); // ClassCastException - ls contains
                      // Objects, not Strings.
```

So, without some special dispensation, we can see that the call from `reverse()` to `rev()` would be disallowed. If this were the case, the author of `reverse()` would be forced to write its signature as:

```
public static <T> void reverse(List<T> list)
```

This is undesirable, as it exposes implementation information to the caller. Worse, the designer of an API might reason that the signature using a wildcard is what the callers of the API require, and only later realize that a type safe implementation was precluded.

The call from `reverse()` to `rev()` is in fact harmless, but it cannot be justified on the basis of a general subtyping relation between `List<?>` and `List<T>`. The call is harmless, because the incoming argument is doubtless a list of some type (albeit an unknown one). If we can capture this unknown type in a type variable `X`, we can infer `T` to be `X`. That is the essence of capture conversion. The specification of course must cope with complications, like non-trivial (and possibly recursively defined) upper or lower bounds, the presence of multiple arguments etc.

Mathematically sophisticated readers will want to relate capture conversion to established type theory. Readers unfamiliar with type theory can skip this discussion - or else study a suitable text, such as *Types and Programming Languages* by Benjamin Pierce, and then revisit this section.

Here then is a brief summary of the relationship of capture conversion to established type theoretical notions. Wildcard types are a restricted form of existential types. Capture conversion corresponds loosely to an opening of a value of existential type. A capture conversion of an expression `e` can be thought of as an `open` of `e` in a scope that comprises the top level expression that encloses `e`.

The classical `open` operation on existentials requires that the captured type variable must not escape the opened expression. The `open` that corresponds to capture conversion is always on a scope sufficiently large that the captured type variable can never be visible outside that scope. The advantage of this scheme is that there is no need for a `close` operation, as defined in the paper *On Variance-Based Subtyping for Parametric Types* by Atsushi Igarashi and Mirko Viroli, in the proceedings of the 16th European Conference on Object Oriented Programming (ECOOP 2002). For a formal account of wildcards, see *Wild FJ* by Mads Torgersen, Erik Ernst and Christian Plesner Hansen, in the 12th workshop on Foundations of Object Oriented Programming (FOOL 2005).
