Java generics and type erasure

1、 Concept

When the Java language is in a version where generics have not yet appeared, type generalization can only be realized through the combination of the two characteristics of object being the parent class of all types and type casting. One disadvantage of this is that only programmers and runtime virtual machines know what type of object this object is. At compile time, the compiler cannot check whether the cast of this object is successful. Therefore, the risk of many classcastexceptions will be transferred to the program runtime.

Generics is a new feature in JDK 1.5, Its essence is parameterized type (parameterized type), for example, when we define a method, we define a variable, called a formal parameter, and the variable value changes according to the value of the passed in argument. The emergence of generic type is to solve the problem that the type can also change according to the passed in type, that is, the operated data type is specified as a parameter. It is mainly used to define classes, interfaces Method can reduce code duplication.

However, the drawback is that the generic implementation in Java is not a real generic. It only exists in the program source code. In the compiled bytecode file, it has been replaced with the original native type, and the cast code has been inserted in the corresponding place. Therefore, for the Java language at runtime, ArrayList < int > and ArrayList < string > are the same class, This implementation method is called generic type erasure, so generic technology is actually a syntax sugar of the Java language.

Tips: Although grammar candy will not provide practical functional improvements, they can improve work efficiency, improve grammar rigor, or reduce the chance of coding errors. Common syntax sugars include "internal class", "automatic boxing / unpacking", "assertion statement", "enumeration class", etc

2、 Generic usage

1. Identifier meaning in generic

2. Define a generic method

First, the declaration of generics must be after the modifier of the method (public, static, final, abstract, etc.) and before the return value declaration. Multiple generics can be declared, separated by commas.

3. Define a generic class

3、 Generic erase

Next, let's look at an example of generic erasure

Compile this java code into a class file, and then decompile it with the bytecode decompiler. You will find that all generics are missing, and the generic types have changed back to the native types, but the type is cast in the corresponding places.

4、 Generic recognition

As we said above, generics are erased during compilation, Various scenarios during code run time How to identify the parameterized type (e.g. reflection).

Therefore, the so-called erasure of erasure method only erases the bytecode in the code attribute of the method. In fact, generic information is retained in the metadata, which is also the fundamental basis for us to obtain parametric types by reflection and other means.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>