First, the introduction example.
1. Example.
Suppose you now have a ArrayList container, and if you do not use a generic constraint, you can add objects of various types to the container, but if you take out only one type of conversion, you will definitely throw a classcastexception exception.
1 Packagep04. generatetypedemo.demo01;2 3 Importjava.util.ArrayList;4 ImportJava.util.ListIterator;5 6 Public classDemo01 {7 Public Static voidMain (String args[])8 {9ArrayList al=NewArrayList ();TenAl.add ("ABC1"); OneAl.add ("ABC2"); AAl.add (1); -Al.add (2); -Listiterator li=al.listiterator (); the while(Li.hasnext ()) - { -String str=(String) Li.next (); - System.out.println (str); + } - } + A}View Code
Although this program compiles in eclipse without error, the runtime generates a ClassCastException exception, which creates a potential risk. Although Eclipse does not have an error, it uses a small yellow exclamation mark to prompt the programmer to do so with a potential risk. How do you make sure there is a risk at compile time? Generics were created after the JDK1.5.
Generics use <> to represent the type of,<> inside the sky. This allows the container to receive only objects of the specified type.
Change Code:
1 Packagep04. generatetypedemo.demo01;2 3 Importjava.util.ArrayList;4 ImportJava.util.ListIterator;5 6 Public classDemo01 {7 Public Static voidMain (String args[])8 {9Arraylist<string>al=NewArraylist<string>();TenAl.add ("ABC1"); OneAl.add ("ABC2"); AAl.add (1); -Al.add (2); -Listiterator<string> li=al.listiterator (); the while(Li.hasnext ()) - { -String str=(String) Li.next (); - System.out.println (str); + } - } + A}View Code
This code is only a generic type in relation to the previous code, and the rest does not change, but the error has been made without running in eclipse.
This is the biggest benefit of using generics. Also, it should be noted that after using generics, iterators also use generics to define the type of elements that will be iterated, and once this is done, there is no need to do a strong-turn action when the element is removed.
1 Packagep04. generatetypedemo.demo01;2 3 Importjava.util.ArrayList;4 ImportJava.util.ListIterator;5 6 Public classDemo01 {7 Public Static voidMain (String args[])8 {9Arraylist<string>al=NewArraylist<string>();TenAl.add ("ABC1"); OneAl.add ("ABC2"); AAl.add ("ABC3"); -Al.add ("ABC4"); -Listiterator<string> li=al.listiterator (); the while(Li.hasnext ()) - { -String str=Li.next (); - System.out.println (str); + } - } + A}View Code
2. Summary:
What generics are: Generics are a new feature that appears after JDK1.5.
Purpose of using generics: to improve security mechanisms. (JDK upgrade for almost three purposes: improve efficiency, simplify writing, improve security)
Benefits of using generics:
1. Turn the runtime issue classcastexception to the compile time.
2. Avoid the hassle of forced conversions.
Doubts:<> and E
What is <>?
Just as the method uses () to define the range of parameters, generics use <> to define the type of arguments to pass in.
<> when do I use it?
Used when the reference data type of the operation is not deterministic.
What is E?
E represents a parameter, the shorthand for element, and the reason for not using lowercase is that the parameter type represented by E is limited to the reference data type, not the base data type.
3. Erase and compensate for generics.
Erase: Although the programmer used generics when writing the code, the Java compiler generated the class file with the generics removed, and the resulting class file did not have a generic type. This is called introspection erasure.
Compensation: The purpose of the erase is to be compatible with the lower version of the JRE, but there is no way to use the cast in the generic technology to make the lower version supported, so the compiler makes a slight adjustment, which automatically gets the object type (using the GetClass method) and completes the implicit strong-turn action. This is the compensation for generics.
Second, generic type, generic method, generic interface.
1. Generics are used in the TreeSet collection.
TreeSet is a relatively complex container in a collection frame, so use it as a presentation container.
"Java generics"