Java generics implement the concept of "parameterized type", which refers to specifying the data type of an operation as a parameter, which is the most used in a container, such as:list<string> strlist=new arraylist<string > (),list<integer> intlist=new arraylist<integer> (); The data type that the strlist can manipulate is a string type, The data type that intlist can manipulate is type int, and generics are fully applied in the container.
Generics are a great boost to code reuse, and we also think of ways to design code reuse in our programs, such as outputting a content that we want to use SYSTEM.OUT.PRINTLN (...), knocking on so many things or trouble, we can define a println () method, As follows:
static void println (Object o) {
System.out.println (o);
}
When you export something in the program, just call println (...). Can, this println () method has a "generalization" mechanism, it can output any System.out.println () can output something, the method's parameter type is set to the base class object type, we know, The basic data types in Java have corresponding class types of data, such as data of type int has integer class type of data, Java has "Auto boxing" function, that is, the corresponding basic type of data into the corresponding class type of the object, such as the above println (3), After compiling it will become println (integer.valueof (3)), and all Java classes are inherited from the object class, so of course you can use the println () method.
So is it possible to design a method, class, and interface for a generalization type by defining the data type as an object type? The answer is yes, but "too generalized" so that there is no border. Here's the code first:
1 Public classgen{2 3 Public Static voidMain (string[] args) {4MyList list=NewmyList ();5List.push ("HelloWorld");6List.push (12);7 Object Val;8 while((Val=list.pop ())! =NULL){9 System.out.println (Val.getclass ());Ten } One } A } - - classmylist{ the classnode{ -Object val=NULL; -Node next=NULL; - Node (Object val,node next) { + This. val=Val; - This. next=Next; + } A Node () { atVal=NULL; -next=NULL; - } - BooleanEnd () { - returnval==NULL&& next==NULL; - } in } - PrivateNode top=NULL; to myList () { +top=NewNode (); - } the //Insert New Value * Public voidpush (Object val) { $top=NewNode (val,top);Panax Notoginseng } - //returns the value of the head node the PublicObject Pop () { +Object val=Top.val; A if(!Top.end ()) { thetop=Top.next; + } - returnVal; $ } $}View Code
We have customized a list linked list, in order to make this list more universal usability, we define the data inside the list class as Object type, so we can insert string type data, int type data ... But this design is "too generalization", you can even "plug", such as the above execution results, the int type of data and the string type of data inserted together, obviously in the subsequent operation may have an error, for example, you create a self-thought is all int type list and sum them, But accidentally inserted the string type of the number "123", the program can also be compiled through, you think your program is right, the result of the error when running! You are deeply hit, from the south-east Branch, the global computer industry has been down .....
In order to prevent the above tragedies, we still hope that the error will be exposed during the compilation period in order to kill and sprout the error in time, a major function of the generic design is embodied: Specify in advance what type of data our linked list can insert and verify the correctness of its type by the compiler! at this point, we can design:
1 Public classgen{2 Public Static voidMain (string[] args) {3 //This specifies that the data type in List2 is of type string, that is, T is set to string type4Mylist2<string> list2=NewMylist2<string>();5List2.push ("Hello World");6List2.push ("Hello Code");7 //List2.push (+);//errors are alerted at compile time8 String S;9 while((S=list2.pop ())! =NULL){Ten System.out.println (s); One } A } - } - classMylist2<t>{ the classNode<u>{ -U val=NULL; -Node<u> next=NULL; -Node (U val,node<u>next) { + This. val=Val; - This. next=Next; + } A Node () { atVal=NULL; -next=NULL; - } - BooleanEnd () { - returnval==NULL&& next==NULL; - } in}//This sets the value type in node to the U type, which is to pass u to t - PrivateNode<t> top=NULL; to MyList2 () { +top=NewNode<t>(); - } the Public voidpush (T val) { *top=NewNode<t>(val,top); $ }Panax Notoginseng PublicT Pop () { -T val=Top.val; the if(!Top.end ()) { +top=Top.next; A } the returnVal; + } -}View Code
When we accidentally execute List2.push (23), the compiler will remind you in the compile phase: you are wrong! So we were happy to fix the wrong code.
Java container classes can be said to be an important embodiment of Java code reuse design, and the Java container Class design is based on generics, which is the important point of Java generics.
Note: The above code design section is referenced from "Thinking in Java", thanks to Bruce Eckel.
Java Programming generics