The first is really a generic design problem

ArrayList<E>thisEIs a type parameter that specifies the type of the Class to provide the compiler for type erasure, which requires that it be a Class, Object system, the basic data type is not a Class or Object, is a special JDK.

To expand on generics:

  1. Whenever a generic type is defined, a corresponding raw type is automatically provided. The name of the raw type is the name of the generic type after the type parameters are deleted. Such as:

    public class Pair<T> {
        private T first;
        private T second;
        
        public Pair(T first, T second) {
            this.first = first;
            this.second = second;
        }
        
        public T getFirst() {returnfirst; } public TgetSecond() {returnsecond; } public TsetFirst(T val) {this.first = val}
        public T setSecond(T val) {this.second = val}
    }
    ===>
    public class Pair<Object> {
        private Object first;
        private Object second;
        
        public Pair(Object first, Object second) {
            this.first = first;
            this.second = second;
        }
        
        public Object getFirst() {returnfirst; } public ObjectgetSecond() {returnsecond; } public ObjectsetFirst(Object val) {this.first = val}
        public Object setSecond(Object val) {this.second = val}
    }
    Copy the code
    ArrayList<E> -> ArrayList<Object>  
    ArrayList<E extends Comparable> -> ArrayList<Comparable>  
    Pair<E> -> Pair<Object>
    Copy the code

    Here E/T is an untyped variable, so it is replaced by Object

  2. Translate generic expressions

    Pair<Employee> ep = ... ; Employee buddy = ep.getFirst(); //public Object getFirst()Copy the code

    In this case, getFirst returns an Object type because of type erasure. The compiler (or the VIRTUAL machine) automatically casts it. Convert Object to Employee. The underlying operation of the VIRTUAL machine for this problem is divided into two steps:

    1. Call to the original method pair.getFirst ()
    2. Cast the returned Object type to Employee
  3. Translate generic methods

    public static <T extends Comparable> T min(T[] arr)
    ===>
    public static <Comparable> Comparable min(Comparable[] arr)
    Copy the code

Type parameter qualification

class Air<T extends Serializable & Comparable>
Copy the code
  • Type variables can have more than one qualification, separated by &
  • The extends keyword is used whether T is qualified for an interface or a class, which is just a convention
  • In Java’s class and interface design, a class can implement multiple interfaces, but can inherit at most one class. The type parameter here is also a class, so the type qualifier can have at most one class and must be the first in the qualified list, whereas the interface qualifier can have multiple.

Generic conversion

  • There are no generics in the virtual machine, just ordinary classes and methods
  • All type parameters are replaced with their qualified types
  • Bridge methods are synthesized to preserve polymorphism
  • To preserve type safety, insert casts if necessary