java generics wildcards erased? T and thinking? Erase, covariant

Look at the code #

public class ErasedTypeEquivalence {
    public static void main(String[] args) {
        Class c1 = new ArrayList<String>().getClass() ;
        Class c2 = new ArrayList<Integer>().getClass() ;
        System.out.println(c1 == c2);
    }
}

  


// true is equipped with a type of String with a list and a list of type Integer their class objects are equal. Look at the code below to see why they would equal
public class LostInformation {
    public static void main(String[] args) {
        ArrayList<Frob> list = new ArrayList<>() ;
        HashMap<Frob,Fnorkle> map = new HashMap<>() ;
        Quark<Fnorkle> quark = new Quark<>() ;
        Particle<Long,Double> p = new Particle<>() ;
        System.out.println(Arrays.toString(list.getClass().getTypeParameters()));
        System.out.println(Arrays.toString(map.getClass().getTypeParameters()));
        System.out.println(Arrays.toString(quark.getClass().getTypeParameters()));
        System.out.println(Arrays.toString(p.getClass().getTypeParameters()));
    }
}
class Frob{}
class Fnorkle{}
class Quark<Q>{}
Particle class <the Position, the Momentum> {} 
// [E] 
// [K, V] 
// [Q] 
// osition, the Momentum] 
// harsh reality is within generic code, not available in generic parameter type of information. This is the generic erased.

  


Reflection class A <T extends B> and class A {public B b;} What is the difference of both, see the following codes

HASF {class 
    public void F () { 
        System.out.println ( "F ()"); 
    } 
} 
public class Mainpulator2 <HASF the extends T> { 
    Private T obj; 
    public Mainpulator2 (T obj) { 
        this.obj obj = ; 
    } 
    public void Manipulate () { 
        obj.f (); 
    } 
} 
public class Manipulatore3 { 
    Private HASF obj; // HASF holding the object with the object and its subtypes above code plays a role in the same, then why there <T extends HasF> syntax it 
    public Manipulatore3 (HASF obj) { 
        this.obj = obj; 
    } 
    public void Manipulate () { 
        obj.f (); 
    } 
}

  

We can see Mainpulator2 classes are held HasF type and subtype of HasF but you will find Manipulatore3 class holds objects HasF object and its sub-types of the same code above played a role, then why have <T extends HasF> syntax it?

ReturnGenericType class public <T extends HasF> { 
    Private T obj; 
    public ReturnGenericType (T obj) { 
        this.obj = obj; 
    } 
    public GET T () {// when the class is <T extends HasF> generic, the return T can be obtained a specific type of 
        return obj; 
    }

  


}

If a class has a method that returns T, then generics will help. Because they can return the exact type.

Erase compensation (some difficulty, need Duokanjibian)

Erasing lost the ability to code execution which it?

public  class Erased<T> {
    private final int size = 100 ;
    public  void f(Object arg){
        //if (arg instanceof T){} ; //error
      //  T var = new T() ; //error
      //  T[] array = new T[size] ; //error
        T[] array = (T[]) new Object[size]; //unchecked warining
    }
}

  


Wildcard <?>

First, think under List, List <Object>, List <?> Difference

static void main public (String [] args) { 
      // first paragraph: defining a set manner before generics this way we can see, a1 can add different types of objects into account. But this there is a problem, it is time to get out in transition. Therefore, the generic concept came out. 
        A1 = the ArrayList new new List (); 
        a1.add (new new Object ()); 
        a1.add (new new Integer (. 1)); 
        a1.add (new new String ( "123")); 
        // second stage: this assignment is well understood, there is no doubt. 
        A1 = A2 List <Object>; 
        a2.add (new new Object ()); 
        a2.add (new new Integer (222)); 
        a2.add (new new String ( "123")); 
        // third paragraph: The following the assignment may actually succeed. This is not a bit surprised. a1 can hold different types of objects, how can be assigned to a type Integer List to hold it? This is because, due to the generics appear after JDK1.5, taking into account the forward-compatible, so the history of the code sometimes need to assign a new generic code. But obviously this situation is against humanity. But also a problem. 
        List <Integer> A3 = A1; 
        a3.add (new new Integer (123));
        Object //a3.add(new ());
        String //a3.add(new ( "123")); 
        // fourth paragraph: speak out, see the following example explained before 
        List A4 = A1; <?> 
        a1.remove (0); 
        A4. Clear (); 
        //a4.add(new Object ()); 
    }

  

Speaking in front of our second, third way to compile this assignment is an issue left over from history. So definitely do not recommend this way of. So the

List a1 = new ArrayList (); into List <Integer> A1 = new new the ArrayList <Integer> OK?

  
List<Integer> a1 = new ArrayList<Integer>() ;
       // a1.add(new Object()) ;
        a1.add(new Integer(1)) ;
      //  a1.add(new String("123")) ;
​
        List<Object> a2 = a1 ;

  

After testing in this way is not acceptable. Here leads to the concept of covariant type. Covariant types in an array are possible. That array can be so assigned. The collection is not, so here assignment error.

That sometimes we want to put a List <Integer> references assigned to a List <Object> references this time how to do it? So there have been <?> Wildcard. Here you can explain the fourth paragraph, and it also may explain why there is java <?> Wildcard. List a4 = a1 <?>; With this you can assign a wildcard.

There are three wildcard <?> <? Extends T> <? Super T>

These three types may not really understand them easily. You have to remember is that the definition quoted out of these three methods can be assigned to different objects. And you can assign different objects, which is why there is a wildcard mentioned above appear. How assignments look at the following code

 public static void main(String[] args) {
        List<Animal> animals = new ArrayList<Animal>() ;
        List<Cat> cats = new ArrayList<Cat>() ;
        List<Garfield> garfields = new ArrayList<Garfield>() ;
​
        animals.add(new Animal()) ;
        cats.add(new Cat()) ;
        garfields.add(new Garfield()) ;
        //出错,只能赋值Cat或者Cat的子类
     //   List<? extends Cat> extendsCatFormAnimal = animals ;
        List<? super Cat> superCatFormAnimal = animals ;
​
        List<? extends Cat> extendsCatFormCat = cats ;
        //List<? super Cat> superCatFormGarfield = garfields ;
        // add all operations fail. Here you may have a big question. The first line below is easy to understand. But on the second row and third row difficult to understand. Here is get back to what I said above wildcard to solve what appears. extendsCatFormCat references can be assigned cats. Also can be assigned to hold Cat or Cat subtype list. What does that mean? This can be changed later reference. List <? Super Cat> superCatFormAnimal = animals. Can also List superCatFormAnimal = new ArrayList <Garfield> () <super Cat?>; Since the list of references can be assigned to different subclasses of objects. Then you add a reference with new Cat () it is not acceptable. It added new Garfield () is not acceptable. Because if there is a subclass of Garfield class. Similarly, List <? Super Cat> superCatFormAnimal = new ArrayList < subclass Garfield class> (). That superCatFormAnimal This reference does not know what it will be assigned to the list of objects in the future. The lower limit is somewhat similar to the feeling. 
        Animal //extendsCatFormCat.add(new ()); 
        //extendsCatFormCat.add(new Cat ()); 
        //extendsCatFormCat.add(new Garfield ()); 
        
        //superCatFormAnimal.add(new Animal ()); 
        superCatFormAnimal. add (new Cat ()); 
        superCatFormAnimal.
        Cat cat = extendsCatFormCat.get(0);
        Object o = extendsCatFormCat.get(0) ;
    }
​
class Animal{
}
class Cat extends Animal{
}
class Garfield extends Cat{
}
​

  

<? Extend T> <? Super T> both. Can not add a front, a rear can not be removed. That GetFirst together PutFirst.

Reference links:

https://www.cnblogs.com/jpfss/p/11726868.html

https://blog.csdn.net/margin_0px/article/details/82906596

https://www.cnblogs.com/wxw7blog/p/7517343.html

Reference books:

thinking in java

A highly efficient code java development manual.

There is a problem, discuss the comments below.

Guess you like

Origin www.cnblogs.com/MaodunJava/p/11938437.html
Recommended