Generics are a new feature of Java SE 1.5, and the essence of generics is the parameter type, meaning that the data type being manipulated is specified as a number of parameters. Such parameter types can be used in the creation of classes, interfaces, and methods, respectively, as generic classes, generic interfaces, and generic methods.
the advantage of introducing generics into the Java language is that it is safe and easy.
Rules and restrictions
1, generic type parameters can only be a class type (including its own definition of the class), cannot be a simple type.
2, the same generic can be the corresponding multiple version number (because the parameter type is indeterminate), the different version number of the generic class instance is incompatible.
3, the generic type can have more than one number of parameters.
4, the generic type of the parameter can use extends statements, such as <t extends Superclass>. It is customary to call it "bounded type".
5. The type of the generic parameter can also be a wildcard type. For example class<?> ClassType = Class.forName ("java.lang.String").
Restricting generics
We typically define generics in this way: Class generics<t>. In fact, the qualified type here is equivalent to object, which is the same as the "object generic" substance. What is the restriction analogy we want to limit T to the set interface type. Just need to do this: class Generics<t extends collection>. Generic T in such a class can only be an implementation class of the collection interface. Incoming Non-collection interface compilation error.
Note: The <t extends collection> here is limited to using keywordextends, which can be either a class or an interface. But the extends here is not the meaning of the inheritance, it should be understood that the T type is the type that implements the collection interface, or T is the type that inherits the XX class.
public class Collectiongen<t extends collection> {private T t;public Collectiongen (T t) {this.t = T;} Public T Gett () {return t;} public void sett (T t) {this.t = t;} public static void Main (String args[]) {collectiongen<arraylist> genlist = Null;genlist = new Collectiongen<arra Ylist> (New ArrayList ());//The following code cannot be compiled//collectiongen<collection> gencollention = null;//gencollention=new Collectiongen<arraylist> (New ArrayList ()); System.out.println ("Able to compile through and execute!");}}
The code above is capable of being compiled and executed successfully. But the two lines that opened the stare went wrong, because <t extends collection> so defined type, it is limited to construct such an instance of the time T is a certain type, this type implements the Collection interface. The simple sentence is: The above way, the definition and the type of instantiation must be completely consistent.
Wild-Wildcard generics
In order to solve the disadvantage that the type is limited to dead and cannot be determined dynamically based on the instance, the "wildcard generic" is introduced, and the wildcard generic format for the above example is <. Extends Collection>. “? "Represents an unknown type, this type is implemented collection interface. Then the above-implemented way can be written as:
public static void Main (String args[]) {generics<arraylist> genlist = Null;genlist = new Generics<arraylist> ( New ArrayList ());//The following code can be compiled generics<Extends collection> gencollention = null;gencollention=new generics<arraylist> (new ArrayList ()); System.out.println ("Able to compile through and execute!");}
1, assuming that only the < is specified;?
Instead of extends, the default is to agree to object and whatever Java class it is under. That is, casual class.
2. The wildcard generic type is not limited to the downward limit. such as <? Extends Collection>, can also limit upward, such as < Super double>. A representation type can only accept a double and its upper-parent class type, such as number, an instance of type object.
3. Generic class definitions can have multiple generic parameters, separated by commas, and can define generic interfaces, generic methods. These are similar to the usage rules for generics in generic classes.
Multi-Interface Restrictions
Although Java generics simply use extends to represent the original concepts of extends and implements. But still to follow the application of the system, Java can only inherit a class, but can implement multiple interfaces, so one of your type needs to be extends qualified. And there are many types of time. There can only be one class, and the class is written first, and the interface is listed behind. That is: <t extends SomeClass & Interface1 & Interface2 & interface3>
The sample here only demonstrates the type qualification of a generic method, with the same rules for the restriction of type parameters in a generic class. Only the header of the class declaration is added. Such as:
public class Demo<t extends comparable & serializable>{//t types are able to use comparable declarative methods and seriablizable features.}
Finally, a little more emphasis. Is that the most critical data of generics is to improve the security of the code, because it is able to check the code during the compilation period, thus avoiding the very many exceptions that occur during the execution of the strong-turn type.
Understand the purpose of generics, and I believe you know how to use generics!
Beginners learn Java (22)--Another understanding of generics