Understanding the use of generics in java

后端 未结 3 1916
情深已故
情深已故 2021-01-03 00:06

Im trying to understand how class generics work and this bit just doesnt make sense to me.

So for instance if I have the following classes:

class A&l         


        
相关标签:
3条回答
  • 2021-01-03 00:50

    The reasons for this are based on how Java implements generics.

    An Arrays Example

    With arrays you can do this (arrays are covariant as others have explained)

    Integer[] myInts = {1,2,3,4};
    Number[] myNumber = myInts;
    

    But, what would happen if you try to do this?

    Number[0] = 3.14; //attempt of heap pollution
    

    This last line would compile just fine, but if you run this code, you could get an ArrayStoreException. Because you’re trying to put a double into an integer array (regardless of being accessed through a number reference).

    This means that you can fool the compiler, but you cannot fool the runtime type system. And this is so because arrays are what we call reifiable types. This means that at runtime Java knows that this array was actually instantiated as an array of integers which simply happens to be accessed through a reference of type Number[].

    So, as you can see, one thing is the actual type of the object, an another thing is the type of the reference that you use to access it, right?

    The Problem with Java Generics

    Now, the problem with Java generic types is that the type information is discarded by the compiler and it is not available at run time. This process is called type erasure. There are good reason for implementing generics like this in Java, but that's a long story, and it has to do with binary compatibility with pre-existing code.

    But the important point here is that since, at runtime there is no type information, there is no way to ensure that we are not committing heap pollution.

    For instance,

    List<Integer> myInts = new ArrayList<Integer>();
    myInts.add(1);
    myInts.add(2);
    
    List<Number> myNums = myInts; //compiler error
    myNums.add(3.14); //heap polution
    

    If the Java compiler does not stop you from doing this, the runtime type system cannot stop you either, because there is no way, at runtime, to determine that this list was supposed to be a list of integers only. The Java runtime would let you put whatever you want into this list, when it should only contain integers, because when it was created, it was declared as a list of integers.

    As such, the designers of Java made sure that you cannot fool the compiler. If you cannot fool the compiler (as we can do with arrays) you cannot fool the runtime type system either.

    As such, we say that generic types are non-reifiable.

    Evidently, this would hamper polymorphism. Consider the following example:

    static long sum(Number[] numbers) {
       long summation = 0;
       for(Number number : numbers) {
          summation += number.longValue();
       }
       return summation;
    }
    

    Now you could use it like this:

    Integer[] myInts = {1,2,3,4,5};
    Long[] myLongs = {1L, 2L, 3L, 4L, 5L};
    Double[] myDoubles = {1.0, 2.0, 3.0, 4.0, 5.0};
    
    System.out.println(sum(myInts));
    System.out.println(sum(myLongs));
    System.out.println(sum(myDoubles));
    

    But if you attempt to implement the same code with generic collections, you will not succeed:

    static long sum(List<Number> numbers) {
       long summation = 0;
       for(Number number : numbers) {
          summation += number.longValue();
       }
       return summation;
    }
    

    You would get compiler erros if you try to...

    List<Integer> myInts = asList(1,2,3,4,5);
    List<Long> myLongs = asList(1L, 2L, 3L, 4L, 5L);
    List<Double> myDoubles = asList(1.0, 2.0, 3.0, 4.0, 5.0);
    
    System.out.println(sum(myInts)); //compiler error
    System.out.println(sum(myLongs)); //compiler error
    System.out.println(sum(myDoubles)); //compiler error
    

    The solution is to learn to use two powerful features of Java generics known as covariance and contravariance.

    Covariance

    With covariance you can read items from a structure, but you cannot write anything into it. All these are valid declarations.

    List<? extends Number> myNums = new ArrayList<Integer>();
    List<? extends Number> myNums = new ArrayList<Float>()
    List<? extends Number> myNums = new ArrayList<Double>()
    

    And you can read from myNums:

    Number n = myNums.get(0); 
    

    Because you can be sure that whatever the actual list contains, it can be upcasted to a Number (after all anything that extends Number is a Number, right?)

    However, you are not allowed to put anything into a covariant structure.

    myNumst.add(45L); //compiler error
    

    This would not be allowed, because Java cannot guarantee what is the actual type of the object in the generic structure. It can be anything that extends Number, but the compiler cannot be sure. So you can read, but not write.

    Contravariance

    With contravariance you can do the opposite. You can put things into a generic structure, but you cannot read out from it.

    List<Object> myObjs = new List<Object();
    myObjs.add("Luke");
    myObjs.add("Obi-wan");
    
    List<? super Number> myNums = myObjs;
    myNums.add(10);
    myNums.add(3.14);
    

    In this case, the actual nature of the object is a List of Objects, and through contravariance, you can put Numbers into it, basically because all numbers have Object as their common ancestor. As such, all Numbers are objects, and therefore this is valid.

    However, you cannot safely read anything from this contravariant structure assuming that you will get a number.

    Number myNum = myNums.get(0); //compiler-error
    

    As you can see, if the compiler allowed you to write this line, you would get a ClassCastException at runtime.

    Get/Put Principle

    As such, use covariance when you only intend to take generic values out of a structure, use contravariance when you only intend to put generic values into a structure and use the exact generic type when you intend to do both.

    The best example I have is the following that copies any kind of numbers from one list into another list. It only gets items from the source, and it only puts items in the destiny.

    public static void copy(List<? extends Number> source, List<? super Number> destiny) {
        for(Number number : source) {
            destiny.add(number);
        }
    }
    

    Thanks to the powers of covariance and contravariance this works for a case like this:

    List<Integer> myInts = asList(1,2,3,4);
    List<Integer> myDoubles = asList(3.14, 6.28);
    List<Object> myObjs = new ArrayList<Object>();
    
    copy(myInts, myObjs);
    copy(myDoubles, myObjs);
    
    0 讨论(0)
  • 2021-01-03 00:52

    The error message is: The method go(capture#1-of ? extends A) in the type A is not applicable for the arguments (A)

    The reason for it: You are not specific about reference a1. It is defined on a capture of "? extends A" that can never be duplicated. Again, when you create a new A() it passes another capture of the "? extends A", the A itself, that cannot be converted to the former.

    0 讨论(0)
  • 2021-01-03 00:56

    When you declare

    A<? extends A> a1 = new A<A>();
    

    the compiler knows only that a1's type parameter is A or some subclass thereof--it doesn't know which one.

    If you change it to A<A> a1, then it will know that an instance of A is a legal argument. The same applies for a2.

    On a related note, the line

    class B extends A
    

    uses a raw type. You should instead say Class B extends A<SomeType> or Class B<E> extends A<E>.

    0 讨论(0)
提交回复
热议问题