这门语言的设计理念就是帮助你如何更好的抽象
What is the difference between <? super E> and <? extends E>
The first says that it's "some type which is an ancestor of E"; the second says that it's "some type which is a subclass of E". (In both cases E itself is okay.)
Suppose you have a class hierarchy like this:
Parent extends Object
Child extends Parent
and a LinkedBlockingQueue<Parent>
You can construct this passing in a List<Child>
which will copy all the elements safely, because every Child
is a parent. You couldn't pass in a List<Object>
because some elements might not be compatible with Parent
How Java implements generics.
Integer[] myInts = {1, 2, 3, 4};
Number[] myNumber = myInts;
myNumber[0] = 3.14; // attempt to heap pollution
The 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
Arrays are what we call reifiable types 可具体化.
The problem with Java generic types is that the type information is discarded by the compiler and not available at runtime.
List<Integer> myInts = new ArrayList<Integer>();
myInts.add(1);
myInts.add(2);
List<Number> myNums = myInts; //compiler error
myNums.add(3.14); //heap pollution
As such, we say that generic types are non-reifiable.
heap pollution