Java泛型几个常见的术语

Generic type

A generic type is a type with formal type parameters. A parameterized type is an instantiation of a generic type with actual type arguments.

A generic type is a reference type that has one or more type parameters. These type parameters are later replaced by type arguments when the generic type is instantiated (or declared ).

Example (of a generic type):

interface Collection<E> {
public Iterator iterator();
}


The interface Collection has one type parameter E . The type parameter E is a place holder that will later be replaced by a type argument when the generic type is instantiated and used.

Parameterized type

The instantiation of a generic type with actual type arguments is called a parameterized type .

Example of a parameterized type:

Collection<String> coll = new LinkedList<>();


The declaration Collection<String> denotes a parameterized type, which is an instantiation of the generic type Collection , where the place holder E has been replaced by the concrete type String .

Type parameter

A place holder for a type argument.

Generic types have one or more type parameters.

Example of a generic type:

interface Comparable<E> {
int compareTo(E other);
}


The identifier E is a type parameter. Each type parameter is replaced by a type argument when an instantiation of the generic type, such as Comparable<Object> or Comparable<? extends Number> , is used.

Type arguments

Generic types and methods have formal type parameters, which are replaced by actual type arguments when the parameterized type or method is instantiated.

class Box <T> {
private T theObject;
public Box( T arg) { theObject = arg; }
...
}
class Test {
public static void main(String[] args) {
Box <String> box = new Box <String> ("Jack");
}
}


In the example we see a generic class Box with one formal type parameter T. This formal type parameter is replaced by actual type argument String , when the Box type is used in the test program.