The use of generics effectively reduces the code redundancy , Reduce redundant and useless code , Make the code more readable , More concise , At the same time, it also shortens the moving time Repeated labor time of brick workers .

type :
  T: Indicates the type of determination , The most commonly used generic representation .
  ? : Represents the type of uncertainty , Similar to wildcards .
  K: Generally used for key value alignment –> key
  v: Generally used for key value alignment –> value
  E: Represents an enumeration .

Range :
  T: Represents the same type .
  ?: Represents any type .
How to write it :
  T:
  ?:<?>,<? extends Object>,<? super Object>
Pay attention 1:
  <? extends XX>: express XX type , and XX Subtype of .
  <? super XX>: express XX type , and XX Parent type of
such as : If only specified <?>, And No extends, Is allowed by default Object And any Java Class . That is, any class .
Pay attention 2:
  class: When instantiating , Specific types need to be specified .
  class<?>: Can represent all types .

The benefits of generics are :
Start version :
public void write(Integer i, Integer[] ia); public void write(Double d,
Double[] da);
Generic version :
public <T> void write(T t, T[] ta);
T and ? First of all, we should distinguish between two different scenes :
  first , Declare a generic class or method .
  second , Using generic classes or methods .
  Type parameter T Mainly used for the first type , Declare a generic class or method .
  Unbounded wildcards ? Mainly used for the second type , Using generic classes or methods

Subclasses derived from generic classes
There are two situations :
   Subclass specifies the type parameter variable of a generic class
   Subclass ambiguous type parameter variable of generic class
// Define generics on interfaces public interface Inter<T> { public abstract void show(T t); } /**
* Subclass specifies the type parameter variable of a generic class : */ public class InterImpl implements Inter<String> {
@Override public void show(String s) { System.out.println(s); } } /** *
Subclass ambiguous type parameter variable of generic class : * Implementation classes should also be defined <T> Type */ public class InterImpl<T> implements
Inter<T> { @Override public void show(T t) { System.out.println(t); } } public
static void main(String[] args) { // Test the first case //Inter<String> i = new
InterImpl(); //i.show("hello"); // The second case is testing Inter<String> ii = new InterImpl<>();
ii.show("100"); }
It is worth noting that :

* Methods that implement the overriding parent of a class , The return value should be of the same type as the parent class !
* Generics declared on a class are only valid for non static members

Technology