GenericYes
Java SE1.5 of the new features, generic is essentially a parameterized type, that is, the operated data type is specified as a parameter. This type of parameter can be used to create classes, interfaces, and methods, which are called generic classes, generic interfaces, and generic methods.
The advantage of introducing generics in Java is that it is secure and simple.
Before Java SE 1.5, if there is no generics, the parameter "arbitrary" can be implemented by referencing the type object ", the disadvantage of "arbitrary" is explicit forced type conversion, which requires developers to perform with predictable actual parameter types. If the forced type conversion is incorrect, the compiler may not prompt an error and the exception occurs during running. This is a security risk.
The advantage of generics is to check the type security during compilation, and all the forced conversions are both automatic and implicit, improving the code reuse rate.
There are also some rules and restrictions for generic usage:
1. generic type parameters can only be class types (including custom classes), not simple types.
2. The same generic type can correspond to multiple versions (because the parameter type is uncertain), and the generic instances of different versions are incompatible.
3. There can be multiple generic type parameters.
4. You can use the extends statement for generic parameter types, for example, <t extends superclass>. It is a "bounded type ".
5. The generic parameter type can also be a wildcard type. For example, class <?> Classtype = Class. forname (Java. Lang. String );
There are many generic interfaces and methods. It takes some time to understand and use them skillfully. Here are two examples that I used to understand generics (based on my impressions) to implement the same function. One uses generics and the other is not used. Through comparison, you can quickly learn generic applications and learn about generic 70% content.
Example 1: Generic
Public class Gen <t> {
Private t ob; // defines generic member variables
Public Gen (T ob ){
This. Ob = ob;
}
Public t getob (){
Return ob;
}
Public void setob (T ob ){
This. Ob = ob;
}
Public void showtyep (){
System. Out. println ("t actual type:" + OB. getclass (). getname ());
}
}
Public class gendemo {
Public static void main (string [] ARGs ){
// Define an integer version of generic gen
Gen <integer> Gen B = new gen <integer> (88 );
Using B. showtyep ();
Int I = repeated B. getob ();
System. Out. println ("value =" + I );
System. Out. println ("----------------------------------");
// Define a string version of generic type gen
Gen <string> strob = new gen <string> ("Hello Gen! ");
Strob. showtyep ();
String S = strob. getob ();
System. Out. println ("value =" + S );
}
} |
Example 2: no generic type is used
Public class gen2 {
Private object ob; // defines a common type member.
Public gen2 (Object ob ){
This. Ob = ob;
}
Public object getob (){
Return ob;
}
Public void setob (Object ob ){
This. Ob = ob;
}
Public void showtyep (){
System. Out. println ("t actual type:" + OB. getclass (). getname ());
}
}
Public class gendemo2 {
Public static void main (string [] ARGs ){
// Define an integer version of gen2
Gen2 bytes B = new gen2 (New INTEGER (88 ));
Using B. showtyep ();
Int I = (integer) returns B. getob ();
System. Out. println ("value =" + I );
System. Out. println ("----------------------------------");
// Define a string version of gen2
Gen2 strob = new gen2 ("Hello Gen! ");
Strob. showtyep ();
String S = (string) strob. getob ();
System. Out. println ("value =" + S );
}
} |
Running result:
The results of running the demo in the two examples are the same. The console output is as follows:
The actual type of T is:
Java. Lang. Integer
Value = 88
----------------------------------
The actual type of T is: Java. Lang. String
Value = Hello Gen!
Process finished with exit code 0 |
Looking at this, the basic generic applications and code reading will not be a problem in the future.