---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 12 Edition"
java_version: 12
chapter: "Chapter 4. Types, Values, and Variables"
section: "4.5"
heading: "4.5. Parameterized Types"
parent: "Chapter 4. Types, Values, and Variables"
source: https://docs.oracle.com/javase/specs/jls/se12/html/jls-4.html#jls-4.5
id: "4.5"
url: https://jvm.obfuscated.software/jls/se12/4.5.html
raw: https://jvm.obfuscated.software/jls/se12/4.5.md
---

# 4.5. Parameterized Types

A class or interface declaration that is generic ([§8.1.2](8.1.2.md), [§9.1.2](9.1.2.md)) defines a set of *parameterized types*.

A parameterized type is a class or interface type of the form C`<`T_1,...,T_n`>`, where C is the name of a generic type and `<`T_1,...,T_n`>` is a list of type arguments that denote a particular *parameterization* of the generic type.

A generic type has type parameters F_1,...,F_n with corresponding bounds B_1,...,B_n. Each type argument T_i of a parameterized type ranges over all types that are subtypes of all types listed in the corresponding bound. That is, for each bound type S in B_i, T_i is a subtype of S`[F_1:=T_1,...,F_n:=T_n]` ([§4.10](4.10.md)).

A parameterized type C`<`T_1,...,T_n`>` is *well-formed* if all of the following are true:

- C is the name of a generic type.
- The number of type arguments is the same as the number of type parameters in the generic declaration of C.
- When subjected to capture conversion ([§5.1.10](5.1.10.md)) resulting in the type C`<`X_1,...,X_n`>`, each type argument X_i is a subtype of S`[F_1:=X_1,...,F_n:=X_n]` for each bound type S in B_i.

It is a compile-time error if a parameterized type is not well-formed.

In this specification, whenever we speak of a class or interface type, we include the generic version as well, unless explicitly excluded.

Two parameterized types are *provably distinct* if either of the following is true:

- They are parameterizations of distinct generic type declarations.
- Any of their type arguments are provably distinct.

Given the generic types in the examples of [§8.1.2](8.1.2.md), here are some well-formed parameterized types:

- `Seq<String>`
- `Seq<Seq<String>>`
- `Seq<String>.Zipper<Integer>`
- `Pair<String,Integer>`

Here are some incorrect parameterizations of those generic types:

- `Seq<int>` is illegal, as primitive types cannot be type arguments.
- `Pair<String>` is illegal, as there are not enough type arguments.
- `Pair<String,String,String>` is illegal, as there are too many type arguments.

A parameterized type may be an parameterization of a generic class or interface which is nested. For example, if a non-generic class C has a generic member class D`<`T`>`, then C`.`D`<Object>` is a parameterized type. And if a generic class C`<`T`>` has a non-generic member class D, then the member type C`<String>.`D is a parameterized type, even though the class D is not generic.

## 4.5.1. Type Arguments of Parameterized Types

Type arguments may be either reference types or wildcards. Wildcards are useful in situations where only partial knowledge about the type parameter is required.

```
TypeArguments:
  < TypeArgumentList >

TypeArgumentList:
  TypeArgument {, TypeArgument}

TypeArgument:
  ReferenceType
  Wildcard

Wildcard:
  {Annotation} ? [WildcardBounds]

WildcardBounds:
  extends ReferenceType
  super ReferenceType
```

Wildcards may be given explicit bounds, just like regular type variable declarations. An upper bound is signified by the following syntax, where B is the bound:

```
? extends B
```

Unlike ordinary type variables declared in a method signature, no type inference is required when using a wildcard. Consequently, it is permissible to declare lower bounds on a wildcard, using the following syntax, where B is a lower bound:

```
? super B
```

The wildcard `?` `extends` `Object` is equivalent to the unbounded wildcard `?`.

Two type arguments are *provably distinct* if one of the following is true:

- Neither argument is a type variable or wildcard, and the two arguments are not the same type.
- One type argument is a type variable or wildcard, with an upper bound (from capture conversion ([§5.1.10](5.1.10.md)), if necessary) of S; and the other type argument T is not a type variable or wildcard; and neither |S| `<:` |T| nor |T| `<:` |S| ([§4.8](4.8.md), [§4.10](4.10.md)).
- Each type argument is a type variable or wildcard, with upper bounds (from capture conversion, if necessary) of S and T; and neither |S| `<:` |T| nor |T| `<:` |S|.

A type argument T_1 is said to *contain* another type argument T_2, written T_2 `<=` T_1, if the set of types denoted by T_2 is provably a subset of the set of types denoted by T_1 under the reflexive and transitive closure of the following rules (where `<:` denotes subtyping ([§4.10](4.10.md))):

- `?` `extends` T `<=` `?` `extends` S if T `<:` S
- `?` `extends` T `<=` `?`
- `?` `super` T `<=` `?` `super` S if S `<:` T
- `?` `super` T `<=` `?`
- `?` `super` T `<=` `?` `extends` `Object`
- T `<=` T
- T `<=` `?` `extends` T
- T `<=` `?` `super` T

The relationship of wildcards to established type theory is an interesting one, which we briefly allude to here. Wildcards are a restricted form of existential types. Given a generic type declaration G`<`T `extends` B`>`, G`<?>` is roughly analogous to Some X `<:` B. G`<`X`>`.

Historically, wildcards are a direct descendant of the work by Atsushi Igarashi and Mirko Viroli. Readers interested in a more comprehensive discussion should refer to *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)*. This work itself builds upon earlier work by Kresten Thorup and Mads Torgersen (*Unifying Genericity*, ECOOP 99), as well as a long tradition of work on declaration based variance that goes back to Pierre America's work on POOL (OOPSLA 89).

Wildcards differ in certain details from the constructs described in the aforementioned paper, in particular in the use of capture conversion ([§5.1.10](5.1.10.md)) rather than the `close` operation described by Igarashi and Viroli. 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).

**Example 4.5.1-1. Unbounded Wildcards**

```
import java.util.Collection;
import java.util.ArrayList;

class Test {
    static void printCollection(Collection<?> c) {
                                // a wildcard collection
        for (Object o : c) {
            System.out.println(o);
        }
    }

    public static void main(String[] args) {
        Collection<String> cs = new ArrayList<String>();
        cs.add("hello");
        cs.add("world");
        printCollection(cs);
    }
}
```

Note that using `Collection<Object>` as the type of the incoming parameter, `c`, would not be nearly as useful; the method could only be used with an argument expression that had type `Collection<Object>`, which would be quite rare. In contrast, the use of an unbounded wildcard allows any kind of collection to be passed as an argument.

Here is an example where the element type of an array is parameterized by a wildcard:

```
public Method getMethod(Class<?>[] parameterTypes) { ... }
```

**Example 4.5.1-2. Bounded Wildcards**

```
boolean addAll(Collection<? extends E> c)
```

Here, the method is declared within the interface `Collection<E>`, and is designed to add all the elements of its incoming argument to the collection upon which it is invoked. A natural tendency would be to use `Collection<E>` as the type of `c`, but this is unnecessarily restrictive. An alternative would be to declare the method itself to be generic:

```
<T> boolean addAll(Collection<T> c)
```

This version is sufficiently flexible, but note that the type parameter is used only once in the signature. This reflects the fact that the type parameter is not being used to express any kind of interdependency between the type(s) of the argument(s), the return type and/or throws type. In the absence of such interdependency, generic methods are considered bad style, and wildcards are preferred.

```
Reference(T referent, ReferenceQueue<? super T> queue)
```

Here, the referent can be inserted into any queue whose element type is a supertype of the type `T` of the referent; `T` is the lower bound for the wildcard.

## 4.5.2. Members and Constructors of Parameterized Types

Let C be a generic class or interface declaration with type parameters A_1,...,A_n, and let C`<`T_1,...,T_n`>` be a parameterization of C where, for 1 ≤ *i* ≤ *n*, T_i is a type (rather than a wildcard). Then:

- Let `m` be a member or constructor declaration in C, whose type as declared is T ([§8.2](8.2.md), [§8.8.6](8.8.6.md)).

  The type of `m` in C`<`T_1,...,T_n`>` is T`[A_1:=T_1,...,A_n:=T_n]`.

- Let `m` be a member or constructor declaration in D, where D is a class extended by C or an interface implemented by C. Let D`<`U_1,...,U_k`>` be the supertype of C`<`T_1,...,T_n`>` that corresponds to D.

  The type of `m` in C`<`T_1,...,T_n`>` is the type of `m` in D`<`U_1,...,U_k`>`.

If any of the type arguments in the parameterization of C are wildcards, then:

- The types of the fields, methods, and constructors in C`<`T_1,...,T_n`>` are the types of the fields, methods, and constructors in the capture conversion of C`<`T_1,...,T_n`>` ([§5.1.10](5.1.10.md)).
- Let D be a (possibly generic) class or interface declaration in C. Then the type of D in C`<`T_1,...,T_n`>` is D where, if D is generic, all type arguments are unbounded wildcards.

This is of no consequence, as it is impossible to access a member of a parameterized type without performing capture conversion, and it is impossible to use a wildcard after the keyword `new` in a class instance creation expression ([§15.9](15.9.md)).

The sole exception to the previous paragraph is when a nested parameterized type is used as the expression in an `instanceof` operator ([§15.20.2](15.20.2.md)), where capture conversion is not applied.

A `static` member that is declared in a generic type declaration must be referred to using the non-generic type that corresponds to the generic type ([§6.1](6.1.md), [§6.5.5.2](6.5.5.2.md), [§6.5.6.2](6.5.6.2.md)), or a compile-time error occurs.

In other words, it is illegal to refer to a `static` member declared in a generic type declaration by using a parameterized type.
