1 significance
Generic programming means that the code you write can be reused by many different types of objects .
Common applications: ArrayList
2 K T V E? Meaning of object, etc.
Type variables use uppercase form
E–element (used in collections, because elements are stored in the collection)
T–type (Java Class) (can also use adjacent letters U and s) when needed to represent any type S, U, v–2nd, 3rd, 4th types
K–key (Key)
V–value (value)
N–number (numeric type)
? – Indicates an indeterminate Java type (unrestricted wildcard type)
object– is the root class of all classes, objects of any class can be set to the object reference variable, when used may require type casting, but with the use of generic T, E and other identifiers, before the actual use of the type has been determined, do not need to cast a type.
3 Generic methods
public static <T> T GetXXX () {
}
The type variable is placed after the modifier, and the return type precedes
Qualification of variable types
<t extends Comparable> T
A type variable or wildcard can have more than one qualification
T extends comparable & Serializable
Delimited types are separated by &, commas are used to separate type variables
In Java inheritance, you can have as many interface types as you want, but there is at most one class in the qualification, and if you use a class as a qualification, it must be the first in the qualifying list.
4 generic code and virtual machines
Generics in Java are basically implemented at the compiler level.
In the generated Java byte code, the type information in generics is not included .
When you use generics, the type parameters are removed by the compiler at compile time. In fact, the compiler creates a unique bytecode representation for each generic type by code sharing, and maps instances of that generic type to this unique bytecode representation. The mapping of multiple generic class instances to a unique bytecode representation is implemented by type erasue.
Whenever a generic type is defined, it automatically provides a corresponding primitive type (raw type). The name of the original type is the generic type name after the type parameter is deleted. Erase (erased) type variable and replace with qualified type (unqualified variable with object)
4.1
Type Erase
Type erasure refers to associating a generic type instance to the same byte code by merging the type parameters. The compiler generates only one byte code for a generic type and associates its instance to this bytecode. The key to type erasure is to clear the information about the type parameter from the generic type and, if necessary, to add methods for type checking and type conversion. Type erasure can simply be understood as converting generic Java code to plain Java code, except that the compiler is more straightforward and translates generic Java code directly into plain Java bytecode.
The main process for type erasure is as follows:
1. Replace all generic parameters with their leftmost boundary (the top-most parent type) type.
2. Remove all the type parameters.
Ps:
1. There is no generics in the virtual machine, only the normal class and the normal method , all generic class type parameters are erased at compile time, the generic class does not have its own unique class object. For example, there is no list<string>.class or list<integer>.class, and only List.class.
2. When creating a generic object, indicate the type and let the compiler do the parameter check as early as possible (effective Java, 23rd: Do not use the original ecological type in the new code )
3. Do not ignore compiler warning messages, which means that potential classcastexception are waiting for you.
4. Static variables are shared by all instances of the generic class. For classes declared as Myclass<t>, the method of accessing static variables in them is still myclass.mystaticvar. Whether you create an object through new myclass<string> or new myclass<integer>, you are sharing a static variable.
5. Generic type parameters cannot be used in a catch statement for Java exception handling. Because exception handling is performed by the JVM at run time. Because the type information is erased, the JVM cannot differentiate between the two exception types Myexception<string> and myexception<integer>. For the JVM, they are all myexception types. You cannot execute a catch statement that corresponds to an exception.
5 Generic Conversions
There are no generics in the Java Virtual machine, only ordinary classes and methods
All type parameters are replaced with their qualified type
The Bridge method is synthesized to maintain polymorphism
To preserve type safety, insert a forced type conversion if necessary
6 Constraints and limitations
- Type parameters cannot be instantiated with a base type
- Run-time type queries are only available for the original type
- Cannot create an array of parameterized types
- VarArgs warning
- Cannot instantiate type variable
- Invalid type variable in static context of generic class
- You cannot throw or catch instances of a generic class
- Notice the conflict after erasing
7 wildcard type 7.1 <? Extends t>
Represents the upper bounds of the type, which may be a subclass of T or T for a parameterized type
7.2 <? Super T >
Represents the lower bound of a type (called a superclass in Java core), which indicates that the parameterized type is a supertype of this type (the parent type) until the object
Wildcard characters with a super-type qualification can be written to generic objects, and wildcard characters with sub-type qualification can be read from generic objects.
7.3 Unqualified wildcard characters <?>
Ps:
If you want to read data of type T from the collection and cannot write, can I use the? extends wildcard character; (Producer extends)
If you want to write data of type T from the collection and you do not need to read it, you can use it? Super wildcard; (Consumer Super)
If you need to save and fetch, do not use any wildcard characters.
Java Basic Syntax < 12 > Generic programming