1. Introduction The new language ingredients are introduced in JDK 1.5, generics is one of them. Simple generple generics The following code is taken from the List interface of the java.util package and the definition of the Iterator interface: Public interface list {void add (e x); item item ();} public interface iterator {e next (); boolean hasnext ();} Type parameters related to polar brackets It is the new thing introduced by JDK 5, which is the "form type parameter" of the List and Iterator interface (referred to as "Type Gate") declaration. And when the generic declaration list (for example: List ), All types of types (such as e), will be replaced by the "actual type parameters" (referred to as "type", such as Integer). Although the template mechanism in C is imagined in form, It is necessary to note that the generic statement in Java will never be launched in multiple copies when calling: whether it is in the source level, binary grade, or in disk or memory, it will not be expanded! The generic statement will only be Just compile once and generate a class file (Class file), which is exactly the same as a normal class or interface. Type parameters are actually similar to the usual parameters used in the method or constructor. A method can declare it to "Form value parameters", similarly, generic declarations also have its "type parameters"; when the method is called, the actual parameter will replace the formal parameters, then perform the method body, the same, when the generic declaration is called When the actual type parameters replace the type of type parameters. Remarks on naming conventions: Recommended the type of type parameter in the form of refined and concise (e.g., a single character), is recommended, it is best to avoid using uppercase characters so that The parameters of the class or interface are distinguished. Many promotional goods types use e to indicate the type of type of its element. 2. generics and subtyping (generics and subtyping) first look at the following two lines of code: List ls = new arraylist (); // 1List
}} The problem is that the new version is more useful than the old version. Because the old version can use various types of collection as a parameter, the new version can only use collection