Analysis of generic erasure examples for Java programming exploration
1. Problem elicitation
Source code:
Compiled L
Here is the generic erasure of Java, that is, the generic information will be lost after compilation. For the compiled code, only an object is stored, that is, classA and calssb are only ArrayList types after erasure Let's take a more complicated example
2. In depth
2.1. 1 case 1
Source code:
After compilation:
stringList. The add method receives the object type parameter instead of the specified generic string, indicating that the generic information does not exist after compilation
stringList. The get method also takes out an object type. It will be forcibly converted to a generic type when performing assignment. Note that it will be forcibly converted only when performing assignment, that is, when it is used, the reason is stringlist The bytecode corresponding to get (1) is not forced
2.1. Two cases
Source code:
After compilation:
By comparing the two examples, we can draw a conclusion that if you use generics, all objects are treated as objects at runtime, so the methods you can use are object methods, and the compiler will automatically convert to the specified generic type during assignment operation. Another advantage is that you can find the possible errors of downward transformation earlier in the compilation period, because downward transformation is unsafe
2.2. 1 example 3 (erasure with upper bound)
The upper bound is limited by extensions. If the upper bound is used, the generic type can call the method of the upper bound. For example, generic t calls human's say () method
Source code:
After compilation:
Conclusion:
When upper bounds are used, generic erasure is the type of upper bounds, which explains why upper bound methods can be called In addition, it will be automatically forced to the corresponding generic type as in the assignment operation. In the past, it was forced to object, and here it is forced to human. Both are upward transformation and safe operation
2.2. 2 upper bound with wildcard
First of all, generics appear for security and limitation. The difference between this example and the previous one is that they are used in the list? Extensions fruit is literally understood as any subclass including fruit and inherited from fruit, but in fact, the code can only accept null values, and other values are not accepted
reason:
For the Boolean add (E, e) operation, the generic e here is not marked with a specific type, but a placeholder? Instead, the identifier can receive any type, so the received null However, because of this, specific types cannot be accepted, so apple, apple1 and fruit cannot be compiled because such operations are unsafe. In order to avoid different types, they will not be compiled
Source code:
2.3. 1 lower bound with wildcard
The lower bound is specified by the keyword super? The difference between super apple and extend is to specify the base class as fruit and the parent class of fruit Then why not add (fruit)?
reason:
Generics are for security. Possible errors of generics are found during compilation to the greatest extent. Because fruit and its parent classes can be added, there is no unified root for generics here. Therefore, the value added must be absolutely safe. Only apple and its subclasses comply, and the subclasses will be converted to apple storage. Therefore, adding other parent classes is not allowed
Summary:
The emergence of generics is to reduce errors in downward transformation. The purpose of generics is to find errors in transformation as much as possible. Therefore, unsafe operations (considered by the compiler) will be absolutely prohibited, and all stored data are absolutely safe (considered by the compiler)
The above is all about the generic erasure instance parsing of Java programming exploration in this paper. I hope it will be helpful to you. Interested friends can continue to refer to this site: Java programming generic limited code sharing, Java programming through the list interface to add, delete, change and check code examples. If you have any questions, you can leave a message at any time. Xiaobian will reply to you in time. Thank you for your support!