*** Overview
Generics implement the concept of parameterized types so that Code can be applied to multiple types. The initial purpose of generics is to make classes or methods have the widest expression capability, which can be achieved by decoupling the constraints between classes or methods and the types used. When creating an instance of parameterized type, the compiler is responsible for the Transformation Operation for you and ensures the correctness of the type.
* ** The appearance of a simple generic type is promoted for many reasons. The most significant one is to create a container class. Sometimes, you must be able to hold multiple objects at the same time. But usually only containers are used to store one type of objects. One of the main purposes of generics is to specify the object type to be held by the container, and there is a compiler to ensure the correctness of the type.
* ** A single method call of a tuples can return multiple objects. Similar functions are often required. The solution is to create an object and use it to hold multiple objects to be returned. Tuples directly package a group of objects and store a single object. Elements in the container objects are read-only.
* ** Generic interfaces can be used for interfaces. They are used in the same way as classes. Java generic limitations: basic types cannot be used as type parameters.
* ** Whether a generic method has a generic method is irrelevant to whether its class is generic. The generic method enables this method to change independently of the class. You should try to use generic methods instead of generic classes to make it easier to understand. To define a generic method, you only need to place the list of generic parameters before the returned value: <t>
VoidKk (t) {system.
Out. Println (t);} when using a generic class, you must specify the value of the type parameter when creating an object, but do not specify the value of the generic method, the compiler will find the specific type (type parameter inference ). The generic method is the same as the general method. If the basic type is input when kk () is called, the automatic packaging mechanism will intervene and wrap the value of the basic type as the corresponding object. The generic method and automatic packaging mechanism avoid the code that we had to write before. Type inference only applies to the value assignment operation. You can explicitly specify the type in generic methods. F. <person> getperson ()
* ** The type information is lost in the generic code and cannot obtain any information about the generic parameter type. Java generics are implemented using erasure. When using generics, any specific type information is erased and only one object is being used. List <string> and list <integer> are of the same type at runtime. Only when you want to use a type parameter to be more generalized than a specific type (and all its child types) (you want the code to work across multiple classes) Can you use generics.
* ** It is recommended to use array. newinstance () to create arrays in generics. You cannot create a Generic Array. The solution is to use arraylist. The behavior of the array will continue to be obtained, as well as the type security provided by the generic during compilation. The only way to successfully create a generic array is to create a new erased array and then transform it. With erasure, the runtime type of the array can only be object []. To compensate for the erasure, you can pass a type tag in the constructor to restore the data from the erasure so that we can create an array of actual types. The warning generated during the transformation must be squashed by @ suppress warnings. Once the actual type is obtained, it can be returned and the expected result is obtained, as shown below:
Public class genericarraywithtypetoken <t> {
Private T [] array;
@ Suppresswarnings ("unchecked ")
Public genericarraywithtypetoken (class <t> type, int sz ){
Array = (T []) array. newinstance (type, SZ );
}
Public void put (INT index, t item ){
Array [Index] = item;
}
Public t get (INT index) {return array [Index];}
// Expose the underlying representation:
Public T [] rep () {return array ;}
Public static void main (string [] ARGs ){
Genericarraywithtypetoken <integer> Gai =
New genericarraywithtypetoken <integer> (
Integer. Class, 10 );
// This now works:
Integer [] IA = Gai. Rep ();
}
}
Number [] numbers = new integer [3];
Numbers [0] = new INTEGER (0 );
Numbers [1] = new INTEGER (1 );
Numbers [2] = new INTEGER (2 );
Numbers [1] = new double (3.4); // It can be compiled, but arraystoreexception is reported during runtime.
* ** The boundary can be used to set restrictions on the parameter types of generics to forcibly specify the types that can be applied by generics, more importantly, you can call methods based on your own boundary type. Because the erased type information is removed, the methods that can be called with the unbounded generic parameter are only those that can be called with the object, but if this parameter can be limited to a certain type subset, you can use these subsets to call methods. Java generics reuse the extends keyword.
* ** Wildcard
Class fruit {}
Class Apple extends fruit {}
Class Jonathan extends Apple {}
Class orange extends fruit {}
List <fruit> fruit = new arraylist <Apple> (); // an error is returned. You cannot assign a wildcard involving apple to a wildcard involving fruit.
List <? Extends fruit> fruit = new arraylist <Apple> ();
After the wildcard is used, list <? Extends fruit> represents a list of any types from fruit airports. You can only use this list to call the fruit method.
* ** Unbounded wildcards <?> Declaration: You want Java generics to write this code. The native type is not used, but here, generic parameters can hold any type. When processing multiple parameters, it allows a parameter to be of any type and determines a specific type for other parameters. The benefit of replacing the wildcard type with an exact type is that you can use a wildcard parameter to do more, but using a wildcard makes it necessary to accept a wider range of parameterized types as parameters. * ** Question 1: no basic type can be used as a type parameter. You can use the basic type package class and automatic packaging mechanism (the automatic packaging mechanism cannot be applied to arrays ). When considering performance, you can use a container version dedicated to the basic type.