• Why do we need generics?
  • Definition of generic classes
  • Qualified type variable
  • Limitations of generics
  • Inheritance rules for generic types
  • The wildcard
  • How do virtual machines implement generics

1. Why do we need generics?

  • Suitable for multiple data types to execute the same code

Instead of generics, if you overload it, it’s too cumbersome, the code is the same, but if you have one more type you have to define a method.

  • Types in generics are specified at usage time and do not require casts

2. Definition of generic classes

Generics, or parameterized types. So a generic is the type of a parameter. Control the types of parameters that are specifically restricted by the different types specified by the generic without creating new types. When a generic type is used, T is automatically replaced by the type of the object being used.

Generics can be used in classes, interfaces, methods, and the letters T, E, K, and V. They are enclosed in <> and placed after the class name. If there are multiple characters, separate them with commas (,). Generic interfaces are like classes

Implements a generic interface in two ways: 1. Implements the T type used in the name of the class, and specifies its specific type when new

2. Implements (implements); you don’t need to specify its specific type when new

Generic methods:

Be sure to prefix the return value with < T> otherwise it is not a generic method, but one of the parameters of a normal method is generic

3. Qualified type variable

Generics can pass just about anything. There are times when we need to restrict what we pass to fruit, or when we need to implement a Comparable interface, for example, we need to use the restricted type variable extends, inheritance, or technically an extension. (T extends Fruits&Comparable) (T extends Fruits&Comparable) Multiple interfaces can be implemented. Classes go first, interfaces next, and then ampersand



This means that the above methods can only be called if the Comparable interface is implemented.

4. Limitations of generics

  1. You can only use objects, not primitive types. (example: List< int> a)
  2. Type erasure!! The real runtime virtual machine will erase all generic types and convert them to Object, that is, List< String> and List< Integer>, which are actually one type. GetClass () or getClass().getName () is the same. It’s all pointing to the List class instead of the String Integer.)



3. In static, the generic variable of a generic class is invalidated

Reason: The order in which classes are executed is static first, before constructors, so classes don’t have static methods yet, so generic variables on classes are of course useless.



4. Cannot new (instantiate) -data =new T()

5. Cannot capture an instance of a generic class

5. Inheritance rules for generic types

List< Fruit> and List< Apple> have no inheritance relationship. List< Fruit> and List< Apple> are both of type Listl (getClass()), and their arguments are inheritance relationships, but they have no relationship.

Generics can inherit from generics, such as List and ArrayList

6. The wildcard

See 5: Since List< Fruit> and List< Apple> have no inheritance, but we want to use generics where arguments have inheritance, they also have inheritance, we introduce the wildcard “?”

Orange inherits Fruit, but GenericType< Orange> does not inherit from GenericType< Orange>. GenericType<> is an overall type

Wildcard “?” (Only in method!) When the wildcard is used, the inside parameter inherits the outside parameter, and the above method can be called. Extends, X? super X

? Extends X (input? Is a subclass of x, including x.

Used to securely access data, X and its subtypes can be accessed, and non-NULL data cannot be written. (Can get data, can not set data)

Because? Fruit y=new Apple(), Fruit y=new Apple(), Fruit y=new Apple()

The set method just knows that it’s passing in a Fruit subclass

? Super X (input? Is the parent of x, including x.)

Primarily used to safely write data, X and its subtypes can be written

? It’s the Fruit parent, but we don’t know which one it is, so when we get, we can only get the topmost Object.

“Set” can save any “Fruit” subclass, because any “Fruit” subclass can safely convert to “Fruit”. But the parent of Fruit doesn’t know which one it is, so it can’t be rotated.

7. How do virtual machines implement generics

Generics were introduced later in Java. To fit with early Java, generics in Java are pseudo-generics, that is, when compiled into a Class file, the compiler erases the type. Generic types revert back to native types. Finally, force back to the original type where appropriate.

The implementation of generics in the Java language is called type erasers: ArrayList < int > is the same class as ArrayList < String >