In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the example analysis of generics and type erasure in Java grammar, which is very detailed and has a certain reference value. Interested friends must read it!
Generics and type erasure
Generics, a new feature of JDK 1.5, is essentially the application of parameterized types (Parametersized Type), that is, the data type being operated on is specified as a parameter. This parameter type can be used in:
Class
Interface
Method
In the creation of the
Generic class
Generic interface
Generic method
When there is no generic version of Java. You can only pass:
Object is the parent class of all types
Type cast
The two features work together to achieve type generalization. For example, in access to a hash table, the get () method of HashMap is used before JDK 1.5, and the return value is an Object. Since all types in the Java language are maintained by java.lang.Object, it is possible for Object to be transformed into any object. But also because of the infinite possibilities, only programmers and runtime virtual machines know what kind of object the Objet is.
During compilation, the compiler cannot check whether the forced transformation of the Object was successful. If you rely solely on programmers to ensure correctness, the risk of many ClassCastException will be delayed to the run time of the program.
Generics in the Java language are different. They only exist in the program source code. In the compiled bytecode file, they have been replaced with the original native type (Raw Type), and the cast code is inserted in the appropriate place.
Therefore, Araylist and Aralist are the same class for runtime Java. So generics are a syntax of the Java language. Java is called type erasure, and generics based on this method are called pseudo-generics.
Examples before generic erasure
After compiling this piece of Java code into a Class file, and then decompiling it with bytecode, you will find that the generics are gone and change back to the way they were written before Java generics appeared, and the generic types have all changed back to the original type. Such as:
By erasing generics, some of the elegance of generics is lost.
When generics encounter overload 1
Cannot be compiled because the parameters List and List are erased after compilation. It becomes the same native type List, and the erase action causes the signature of the two methods to become exactly the same. At first glance, the reason for not being able to reload has been found, but is that really the case? It can only be said that generics are erased to the same primitive type only in part because they cannot be overloaded when
Generics meet load 2
Due to the introduction of Java generics, method calls in various scenarios (virtual machine parsing, reflection, etc.) may affect the original foundation, such as how to obtain the incoming parameterized types in the generic class. Therefore, the JCP organization has made corresponding changes to the virtual machine specification, introducing new attributes such as Signature and LocalVariableTypeTable to solve the problem of parameter type identification that accompanies generics. Signature is one of the most important attributes, and its function is to store the signature of a method at the bytecode level. The parameter type stored in this attribute is not the native type, but includes the information of the parameterized type. The revised virtual machine specification requires that all virtual machines that can recognize more than 49.0 Class files should be able to correctly identify Signature parameters.
From the appearance of the Signature attribute, we can also conclude that the so-called erasure is only to erase the bytecode in the Code property of the method. In fact, the metadata still retains the generic information, which is the fundamental basis on which we can obtain the parameterized type through reflection.
Automatic boxing: unpacking and traversing cycles
Automatic boxing: after unpacking and traversing cycle compilation
The traversal loop restores the code to the implementation of the iterator, which is why the class that the traversal loop needs to traverse implements the Iterable interface. Finally, let's take a look at the variable length parameter, which becomes an array type parameter when called, and the programmer used the array to accomplish similar functions before the variable length parameter appeared.
The above is all the content of the article "sample Analysis of generics and Type erasure in Java Grammar". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.