Detailed Explanation of java Pseudo generic Knowledge Points

  • 2021-10-11 18:15:39
  • OfStack

Description

1. Generics in Java are pseudo-generics. This generic implementation method is called type erasure, and generics implemented based on this method are called pseudo-generics.

2. Since Java's generics work only at compile stage, they are used as a check when writing code. When the code runs, there is no generics inside it.

Instances


List<String> l1 = new ArrayList<String>();
List<Integer> l2 = new ArrayList<Integer>();
System.out.println(l1.getClass() == l2.getClass()); //true

The results were true, List < String > And List < Integer > Class in jvm is List. class, meaning that generic information is erased.

Expansion of basic knowledge points:

Basic concepts of generics

Definition of generics: Generics is a new feature of JDK 1.5. Its essence is the application of parameterized types (Parameterized Type), that is to say, the data type operated is specified as a parameter, and the specific type is specified when it is used. This parameter type can be used in the creation of classes interfaces and methods called generic classes generic interfaces and generic methods respectively.

The idea of generics began to take root in the template of C + + language (Templates). When Java language is in the version without generics, it can only realize type generalization through the cooperation of Object as the parent class of all types and type casting. For example, in the access of hash table, get () method of HashMap was used before JDK 1.5, and the return value was an Object object. Because all types in Java language inherit from java. lang. Object, it is possible for Object to be transformed into any object. But because there are infinite possibilities, only programmers and runtime virtual machines know what kind of object this Object is. During compilation, the compiler cannot check whether the forced transformation of Object is successful. If only the programmer is relied on to ensure the correctness of this operation, many risks of ClassCastException will be passed on to the runtime of the program.

Generic technology in C # and Java seem to use the same way, but there are fundamental differences in implementation. Generic in C # whether in the program source code, compiled IL (ES60Language, intermediate language, at this time generic is a placeholder) or runtime CLR are real, List < int > And List < String > Are two different types that are generated at runtime and have their own virtual method tables and type data. This implementation is called type inflation, and the generics implemented based on this method are called real generics.

The generic type in Java language is not like, it only exists in the program source code, in the compiled bytecode file, it has been replaced with the original type (Raw Type, also known as bare type), and in the corresponding place insert forced transformation code, so for the runtime Java language, ArrayList < int > And ArrayList < String > Is the same class. Therefore, generic technology is actually a syntax sugar of Java language. The generic implementation method in Java language is called type erasure, and the generic implemented based on this method is called pseudo-generic. (Type erasure is being learned later)
Program code written using generic mechanisms is more secure and readable than code that clutters with Object variables and then casts them. Generics are especially useful for collection classes.

Generic programming (Generic Programming) means that you write code that can be reused by many different types of objects.


Related articles: