One, the covariance of the array (covariant array type) and the non-covariance of the set
A circle class and a square class inherit from the shape class.
For the covariance of arrays, look at the code:
Public Static Double Totalarea (shape[] arr) { double total = 0; for (Shape shape:arr) { ifnull) + = shape.area (); } return total ; }
If you pass an array of type circle[] to Totalarray (shape[] arr), it is possible to compile the pass and run normally. In other words: circle[] is-a shape[]
For covariance of sets, look at the code:
Public Static double Totalarea (collection<shape> arr) { double total = 0; for (Shape shape:arr) { ifnull) + = shape.area (); } return total ; }
This is not possible if you pass a collection of collection<circle> types to Totalarea (collection<shape> arr) . The compiler will report the following error:
The method Totalarea (Collection<Shape>) in the type Demo isn't applicable for the Argume NTS (Collection<Circle>)
Which means:collection<circle> is-not-a collection<shape>.
Two, what if the non-covariance of the solution set is not flexible?
There is a generic type!
Public Static Double extends Shape> arr) { double total = 0; for (Shape shape:arr) { ifnull) + = shape.area (); } return total ; }
In this way, you can give Totalarea (collection< extends shape> arr)
Passed parameters of the collection<circle>, collection<square>, collection<shape> types.
Third, generic type erase and type erase ClassCastException exception
Java generics exist only at the compile-time level and are not visible to the runtime.
Let's compare them with arrays:
1 New String[10]; 2 object[] obj = str;//upward transformation 34//Exception in thread "main" Java.lang.ArrayStoreException:java.lang.Integer5new Integer (2);
The 5th line of code at run time throws the exception represented in line 4th.
Then look at generics:
1Arraylist<integer> intlist =NewArraylist<integer>();2Intlist.add (2);3Object obj =intlist;4 5 //Type safety:unchecked cast from Object to Arraylist<string>6Arraylist<string> strlist = (arraylist<string>) obj;7 8 //Exception in thread "main" Java.lang.ClassCastException:java.lang.Integer cannot is cast to java.lang.String9String str = strlist.get (0);TenStr.trim ();//Do something with str
The compiler will alert you to line 6th, which is shown in line 5th. The classcastexception exception is thrown when the program runs to the 9th row. Because the ArrayList store is essentially an integer.
Now analyze the 6th line of code:
obj is a reference to the object type, and Strlist is a reference to a arraylist<string> type, so the compiler gives a warning when the transition is down, and at run time, because of the type erase, the equivalent
ArrayList strlist = (ArrayList) obj;
As a result, the code runs to line 6th and can pass.
For the 9th line of code:
Strlist is a arraylist<string> type reference and can of course call ArrayList's Get method. Therefore, there is no problem at compile time. But at run time,
Because, string str = strlist.get (0), it is compiled into a string str = (string) strlist.get (0);
Instead, Strlist.get (0) Gets an integer object and assigns it to string str, due to the integer is-not-a string. So throw classcastexception.
Reference:
Four, why generic arrays are not supported
Refer to the following code:
1 //cannot create a generic array of arraylist<integer>2arraylist<integer>[] Intarr =NewArraylist<integer>[10];3object[] obj =intarr;4 5Arraylist<string> Liststr =NewArraylist<string>();6Obj[0] =Liststr;7 8arraylist<integer> Listint = intarr[0];9Integer i = listint.get (0);//Want an integer, but it is a string
Assuming that the generic array is allowed, then the 2nd line is correct, then there will be no compilation errors as shown in line 1th.
Then the intarr can be transformed into object[], and then object[] put arraylist<string> not what we want arraylist<integer>
Therefore, at run time, the type is erased, and the runtime system cannot check the types stored in the array. It sees only: ArrayList objects are placed inside the intarr array.
Equivalent:
// arraylist<string> liststr = new arraylist<string> (); New ArrayList ();//What is seen at run time // arraylist<integer> listint = intarr[0]; ArrayList Listint = intarr[0];//the runtime sees
In line 9th above, if changed to:
Object o = listint.get (0); // Do something with O
We thought that object o actually refers to an integer type, but it is a string type, and if Hashcode () is called, we assume it executes an integer hashcode (), but it executes a string of hashcode ( That means you can't find a mistake ....
Therefore, Java does not support generic arrays.
Reference:
Http://www.blogjava.net/deepnighttwo/articles/298426.html
Http://www.blogjava.net/sean/archive/2005/08/09/9630.html
Type erasure in Java generics and why generic arrays are not supported