Generic is the Java SE 1.5 new features, the nature of generic type parameters, that is operated by the data type is designated as a parameter. This parameter type can be used in class, the creation of interfaces and methods of the respective category as generic, generic interfaces, generic approach.

Java language advantages of the introduction of generic security simple.

Before in the Java SE 1.5, the situation is not generic, by reference to the type of Object parameters to achieve the “arbitrary” and “arbitrary” and bring the disadvantage is that mandatory to do explicit type conversion, which conversion is to ask the developers of the actual parameters of the type of unforeseen circumstances can be carried out. The mandatory type conversion error, the compiler may not be prompted error when running unusual, this is a security risk.

The advantage of generics at compile time to check the type of security, and forced conversion are all automatic and implicit, and to improve the rate of code reuse.

Generic in use there are some rules and restrictions:

1, the generic type parameter can only be a class type (including custom type), can not be a simple type.

2, with a corresponding number of generic versions can be (because the type is uncertain parameters), different versions of the generic category of examples is not compatible.

3, generic type parameter can have more than one.

4, the parameters of generic types can use the extends statement, for example, <T extends superclass>. Become accustomed to “bounded type.”

5, the generic type parameter type can also be a wildcard. For instance, Class <?> ClassType = Class.forName (java.lang.String);

There are generic interfaces, methods, etc., as many take some martial arts and skilled in order to understand and master the application. In this paper I have time to write a generic understanding of the two examples (see the impression in accordance with written), to achieve the same function, the use of a generic, one did not use, by contrast, can quickly learn the generic applications, the Institute learned that basically 70% of the contents of generic.

One example: the use of generic

public class Gen <T> (
Private T ob; / / define generic member variable

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 is the actual type:” + ob.getClass (). GetName ());
)
)

public class GenDemo (
Public static void main (String [] args) (
/ / Definition of a generic type Integer version of Gen
Gen <Integer> intOb = new Gen <Integer> (88);
IntOb.showTyep ();
Int i = intOb.getOb ();
System.out.println ( “value =” + i);

System.out.println (“———————————-”);

/ / Definition of a generic type String version of Gen
Gen <String> strOb = new Gen <String> ( “Hello Gen!”);
StrOb.showTyep ();
String s = strOb.getOb ();
System.out.println ( “value =” + s);
)
)

Second example: do not use the generic

public class Gen2 (
private Object ob; / / definition of a common type of the members of

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 is the actual type:” + ob.getClass (). GetName ());
)
)

public class GenDemo2 (
Public static void main (String [] args) (
/ / Definition of an Integer-type version of Gen2
Gen2 intOb = new Gen2 (new Integer (88));
IntOb.showTyep ();
Int i = (Integer) intOb.getOb ();
System.out.println ( “value =” + i);

System.out.println (“———————————-”);

/ / Define category Gen2 version of a String
Gen2 strOb = new Gen2 ( “Hello Gen!”);
StrOb.showTyep ();
String s = (String) strOb.getOb ();
System.out.println ( “value =” + s);
)
)

Results:

Demo the results of running two examples are the same, the console output is as follows:

The actual type of T is:

java.lang.Integer
value = 88
———————————-
T is the actual type: java.lang.String
value = Hello Gen!

Process finished with exit code 0

Understand this, since the basic applications and the generic code readers can not be a problem.