Generics are security mechanisms that emerge after JDK1.5, simplifying mechanisms and improving security
Benefits of Generics
1. Transfer problem classcastexception at runtime to compile time
2. Avoid the hassle of forced conversions
<> is used when the reference data type of the operation is indeterminate, import the reference data type that will be manipulated, in fact <> is a parameter range that receives the specific reference data type
When writing a program, it is necessary to explicitly specify the specific reference data type of the descendant as long as the class or interface with <> is used.
Import Java.util.arraylist;import Java.util.iterator;public class Main {<span style= "White-space:pre" ></ Span>public static void Main (string[] args) {<span style= "White-space:pre" ></span>arraylist<string > al = new arraylist<string> ();//clear ArrayList set only put string <span style= "White-space:pre" ></span>al.add ("abc"); <span style= "White-space:pre" ></span>al.add ("SD"); <span style= "White-space:pre" ></ Span>iterator<string> it = Al.iterator ();//The iterator is also clear, the following does not require a strong turn <span style= "White-space:pre" ></span >while (It.hasnext ()) {<span style= "white-space:pre" ></span>string str = It.next (); <span style= " White-space:pre "></span>system.out.println (str); <span style=" White-space:pre "></span>}< Span style= "White-space:pre" ></SPAN>}}
generic technology is used by compilers for compile time, to ensure class security, at run time, the generated class file is non-generic, will be removed generics, this is the generic erase, the reason for Erasure is to be compatible with the loader of the running class (if not erase also means that the loader also to upgrade, so to erase)
Because of the generic erase, it also leads to the generic compensation technology, at run time, by acquiring the type of the element to convert the action, there is no need to cast a re-
The application of generics in the collection
Import Java.util.iterator;import Java.util.treeset;public class man implements Comparable<man>{private int age; private string Name;public man () {super ();//TODO auto-generated constructor stub}public Man (String Name,int age) {super () ; this.age = Age;this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String GetName () {return name;} @Overridepublic boolean equals (Object obj) {//TODO auto-generated method Stubreturn super.equals (obj);} public void SetName (String name) {this.name = name;} @Overridepublic int compareTo (man m) {//TODO auto-generated method Stubint t = this.name.compareTo (m.name); return t==0?t: This.age-m.age;}} public class Main {public static void main (string[] args) {treeset<man> ts = new treeset<man> ();//You can construct a comparator TS. Add (New Man ("a", 1)), Ts.add ("a", 4), Ts.add (New Man ("B", 5)), Ts.add (The New Man ("D", 2)), Ts.add (The New Man ("C", 7)); Terator<man> it = Ts.iterator (); while (It.hasnext ()) {Man m = It.next (); SysteM.out.println (M.getname () + "," +m.getage ());}}}
Custom generic Classes
Defines a tool class that manipulates all objects/*public class Tool {Private Object obj;//in order to improve extensibility with Objectpublic object Getobj () {return obj;} public void Setobj (Object obj) {this.obj = obj;}} *///jdk1.5 a custom generic class later, when the reference data type of the operation in the class is undefined, the generic is used to represent the public class Tool<e> {private E e;public e getobj () {return E;} public void SetObject (E obj) {this.e = obj;}} public class Student extends Man{//man class has public Student () {super ();//TODO auto-generated constructor Stub}public stude NT (String name, int age) {Super (name, age);//TODO auto-generated constructor Stub}}public class Worker extends man {publi C worker () {super ();//Todo auto-generated constructor stub}public Worker (String name, int age) {Super (name, age);//TODO Auto-generated constructor Stub}}public class Main {public static void main (string[] args) {//jdk1.5 before/*tool to = new Too L (); To.setobj (new Worker ());//Once you accidentally pass a worker, compile, pass, run, exception classcastexceptionstudent student = (student) to.getobj () ;//But need strong turn *///jdk1.5 after tool<student> to = new tool<student> (); to.setobjecT (new Student ());//Once you accidentally pass a worker, the compilation will not pass, and it will embody the benefits of generics, detection refers to the compile time Student Student = To.getobj (); System.out.println (Student.getclass ());}}
generic methods: Defining Generics in Methods
Class Tool<e> {Private E e;public e getobj () {return E;} public void SetObject (E obj) {this.e = obj;} public<o> void Show (O str) {//defines the generic on the method, what type is passed, and what type System.out.println ("Tool.show ()" +str) is printed; public<o> void printf (O str) {System.out.println ("tool.printf ()" +str); public static void Mhtod (E e) {} if the generic is defined on a static access class, compilation fails, static is not required for the object//modification, only the generic definition method on public static <F> void Mthod (F str) {System.out.println ("Tool.mthod ()" +str);} }public class Main {public static void Main (string[] args) {tool<string> to = new tool<string> (); To.show ("ASD" ); To.show (New Integer (5)); to.printf ("pri");//While printf is not the only other string Tool.mthod ("haha"); Tool.mthod (' F ');}}
The drawbacks of generics
In the Show method above, if you pass in a string, you cannot use the Str.length () method, because the type of the generic is indeterminate, you cannot determine the method to a class, only some methods of object are used
Java Learning Lesson 41st-Basic application of generics (i)