Java generics are similar to common classes in the form of code introduction. They can be used to declare object and method parameters and return value types, or even force type conversion. Therefore, it is easy to mistakenly think that the generic type is a "class name variable". When a generic class is parameterized, this "variable" is assigned a value to the actual class. In fact, this recognition is a misunderstanding.
Theoretically, classes in Java are composed of corresponding classes. the bytecode of the class file is defined, and the class will be generated during its compilation. the class file has nothing to do with how it is referenced, and of course it is not likely to be related to the parameters at the time of reference. When writing generic classes, you cannot obtain the parameter classes for actual reference, such as class clz = T. statements such as class cannot be compiled (this is not surprising considering that the parameter class can be a wildcard when being referenced ).
Since generics are not classes and cannot obtain the parameter classes at the time of reference, what types of objects declared with generics are compiled? The answer is the upper bound class when the wildcard is declared. To verify this, we can see that the simplest custom generic class only contains the attribute t of a generic declaration and its get and set methods:
You can use the javap tool provided by JDK to view the assembly code of the. Class file. The result is as follows:
We can see that the generic t used in the class body is replaced with the upper bound class number after compilation, and the generic features are completely erased after compilation. If you decompile the genericclasstest. Class file, you can see it more clearly:
In view of this feature, especially when writing generic class codeExercise caution when using generics for forced type conversionSuch as t = (t) (object); such statements will be considered as forced type conversion to generic t during compilationUpper Bound. Since the compiler does not know the exact type of T, this conversion cannot use the instanceof keyword to determine the matching in advance (instanceof t cannot be compiled ). Therefore, improper use may cause an exception in forced type conversion, especially when the converted result is referenced. The following is an example of a deliberate error:
The above generic class forces a value for the generic attribute T in the get method. Since the upper bound class of T compilation is object, this statement can be compiled (but it will trigger the "untested type conversion" warning ). During the reference, a GCT object with Integer as the parameter is declared. In the scope (main function) of GCT, its attribute T is considered to be of the integer type by the compiler, therefore, assigning the value to the integer object I does not cause any compilation problems. However, a type conversion exception occurs during running.
Exception in thread "Main" Java. Lang. classcastexception: Java. Lang. String cannot be cast to Java. Lang. Integer
At test. genericclasstest. Main (genericclasstest. Java: 13)
Since the compilation of a generic class is only related to the upper limit of the generic class, why does one use the object of the generic class, is it strongly typed? (for example, an integer cannot be added to an object declared by an arraylist <string> )? In fact, this is where the Java compilerReferenceDuring the generic class, an escape is performed. To do this, write a Test method:
The method declares a generic genericclasstest object, the reference parameter is integer, And the set and get methods are executed once. Decompile the. Class file using the following code:
As you can see, there are no generic features in the code, which indicates that the compiler has done a step of "preprocessing" before official compilation ", forcibly convert objects involved in generic references to the type specified by generic parameters, that is, the integer class (Note: if a declared generic object is a parameter with a wildcard, only one get and set method can be referenced. Only the get method can be used when the upper bound is specified, and only the set method can be used when the lower bound is specified. Otherwise, the compilation fails. The reason involves the wildcard capture concept, see http://www.ibm.com/developerworks/cn/java/j-jtp04298.html)
The compiler then implements "Real compilation" to generate the. Class file.
What is the purpose of this preprocessing? The advantage is that the CompilerWrite a set of specifications to implement forced type conversionSo that the wildcard with parameters can be used as a strong type from the external layer. If an incorrect type operation exists, an error is reported in the compilation phase.