java Generic erase :
What is a generic wipe?
First look at what generics are. My personal understanding: Because the collection can store arbitrary types of objects. However, once the object type stored in the collection is determined, it is not possible to store other types of objects, otherwise the compilation will not error. However, classcastexception exceptions are thrown when executed. To solve this problem, introduce generics, specifying the type of objects stored in the collection object, so that once other object types are stored in the collection, the compilation will prompt for errors! (Improper understanding of the place.) Hope the Great God more guidance).
The erasure of generics means. The object type specified in the collection at compile time, but the object type that can be stored in the collection is erased at execution time!
For example, it is:
[HTML]View Plaincopy
- Import java.awt.List;
- Import java.util.ArrayList;
- public class Genericdemo {
- public static void Main (string[] args) {
- ArrayList<String> a = NewArrayList<String>();
- Only objects of type string can be stored in collection a
- A.add ("abc");
- A.add (6); Error. Stare out while performing
- ArrayList<Integer> b = NewArrayList<Integer>();
- Only objects of type integer can be stored in collection B
- B.add (6);
- B.add ("abc"); Error. Stare out while performing
- System.out.println (a.getclass () = = B.getclass ());
- Output is True
- /*
- * Cause: Generic execution with erase function; ArrayList<String>and ArrayList<Integer>Erase
- * After the type is ArrayList
- */
- }
- }
each class has a class attribute that, at the same time, demonstrates that using generics does not change the return value of the Class property.
Note: Java cannot create generic arrays: Since arrays in Java are created dynamically by reflection, generics erase type information at execution time, so there is no type information. Of course, it is not possible to construct an array.
What is wrong with the above words or the understanding of the problem, I hope to pass the great God generous enlighten!
Java Generic Erase