I. What are generics?

Since JDK1.5, Java has solved the problem of type safety with generics, which are essentially parameterized types that take the data types you operate on as parameters.

What are the functions of generics?

  1. Using generics allows you to write more flexible, generic code.
  2. Generics advance security checks on code from run time to compile time.
  3. Generics can be cast without casting because the exact type is known at compile time, so you don’t have to cast it yourself.

What are the specific implementations of generics?

A generic class:

Generic types are used in the definition of classes and are called generic classes. Generics allow you to operate on a set of classes by opening up the same interface to the public. The most typical is a variety of container classes, such as List, Set, Map.

Generic class instances:

// Here T can be written as an arbitrary identifier. Common parameters of the form T, E, K, V are often used to represent generics
// When you instantiate a generic class, you must specify the concrete type of T
public class Generic<T>{ 
    // Key is a member variable of type T, whose type is externally specified
    private T key;

    public Generic(T key) { // The generic constructor parameter key is also of type T. The type of T is externally specified
        this.key = key;
    }

    public T getKey(a){ // The generic getKey method returns a value of type T, whose type is externally specified
        returnkey; }}Copy the code

Generic methods:

In Java, the definition of generic classes is simple, but generic methods are more complex.

In particular, most of the member methods we see in generic classes also use generic types, and some even include generic methods in generic classes, so it is very easy for beginners to get generic methods wrong.

Generic classes, which specify the specific type of the generic type when the class is instantiated; A generic method specifies the specific type of the generic type when the method is called.

/** * Basic introduction to generic methods *@paramThe generic argument * passed to tClass@return* 1) the <T> between public and the return value is very important, which can be understood as declaring this method as a generic method. * 2) Only methods that declare <T> are generic methods. Member methods that use a generic type are not generic methods. * 3) <T> indicates that the method will use the generic type T, at which point you can use the generic type T in the method. * 4) As in the definition of generic classes, T can be written as an arbitrary identifier. Common parameters of the form T, E, K, V and so on are often used to represent generics. * /
public <T> T genericMethod(Class<T> tClass)throws InstantiationException ,
  IllegalAccessException{
        T instance = tClass.newInstance();
        return instance;
}

Copy the code

Generic interfaces:

// Define a generic interface
public interface Generator<T> {
    public T next(a);
}

Copy the code

Common representations: < E >, < T>, < K >, < V> > can represent any type

4. Generic erasure

Generics only exist during code compilation, and before entering the JVM, information about generics is erased, also known as type erasure.

Here’s a classic quiz question:

List<String> l1 = new ArrayList<String>(); List<Integer> l2 = new ArrayList<Integer>(); System.out.println(l1.getClass() == l2.getClass()); A true result will become list.class in the JVM after compilationCopy the code
  • If the generic parameter is unbounded, the compiler replaces it with Object.
  • If a generic parameter is bounded, the compiler replaces it with a bounding type
public class TypeErasureDemo {
    public <T> void forEach(Collection<T> collection) {}  //1

    public <E extends String> void iter(Collection<E> collection) {} //2
}
Copy the code

For example, in one case, the generic type is replaced by Object at compile time, and in two cases, the generic type is replaced by String at compile time