In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about what is the constant pool of Class files in the JVM virtual machine. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
I. the function of constant pool
The magic number, minor version number, and major version in the class file are followed by the data area of the constant pool, as shown in the following figure, which is included with a red line:
The constant pool can be compared to the resource warehouse in the Class file. It is the data most associated with other projects in the Class file structure, and it is usually one of the data items that take up the largest space in the Class file. In addition, it is also the first table type data project to appear in the Class file.
Second, the structure of constant pool
The structure of a constant pool is relatively simple. The first two bytes are called constant pool counters (constantpoolcount), which record the number of constant pool entries (cpinfo). This is followed by constantpoolcount-1 constant pool items (cpinfo). This is similar to the message communication protocol, which first defines the size of the message, and then the message content of the corresponding size.
Because the number of constants in the constant pool is not fixed, you need to place a U2 type of data at the entrance of the constant pool, which represents the constant pool capacity count value (constantpoolcount). Unlike the language habit in Java, this capacity count starts at 1 instead of 0. As shown in the figure above, the constant pool capacity (offset address: 0x00000008) is the hexadecimal number 0x0021, that is, 33 in decimal, which means that there are 33 constants in the constant pool, with index values ranging from 1 to 33. At the time of the formulation of the Class file format specification, the designer had a special consideration for vacating the item 0 constant. The purpose of this is that if some data pointing to the index value of the constant pool needs to express the meaning of "do not reference any constant pool project" under certain circumstances, the index value can be set to 0. In the Class file structure, only constant pools start at 1, and for other collection types, including interface index collections, field table collections, method table collections, and so on, the capacity counts are the same as usual, starting at 0.
III. Classification of constant pools
There are two main types of constants stored in the constant pool: literal quantities (Literal) and symbolic references (SymbolicReferences). Literals are relatively close to the constant concepts at the Java language level, such as text strings, constant values declared as final, etc., while symbolic references belong to the concept of compilation principles. The specific classification is shown in the following figure:
Each constant in the constant pool is a table. At first, there are 11 kinds of table structure data with different structures in the constant table. Later, in order to better support dynamic language calls, four additional dynamic language-related constants are added. In order to support the Java modular system (Jigsaw), CONSTANTM oduleinfo and CONSTANTPackageinfo are added, so as of JDK 13, there are 17 different types of constants in the constant table. All 17 types of tables have a common feature. The first bit at the beginning of the table structure is a flag bit of type U1 (tag, for values, see the flag column in the table below), which represents which constant type the current constant belongs to.
The latest on the official website: http://cr.openjdk.java.net/~jrose/jvm/constant-dynamic-jrose.html
When we analyze the class file, we can see that the first 8 bytes are the magic number and version number of the class file, followed by a hexadecimal number 0x0021, that is, 33 in decimal, which means that z, the constant pool of the class file, has 32 constants with an index value of 1: 32. Then there is the first constant, as mentioned above, the first bit of each type of constant is a flag bit of U1 type, representing the type of the constant, here is 0x0a, decimal 10, look up the table above, we can see that it is CONSTANTMethodrefinfo, indicating that this constant is the symbolic application of methods in the class. The specific structure of this type of constant is:
The first index value is 0x0004, which points to the fourth constant in the constant pool, and the second index is 0x0019, which points to the 25th constant in the constant pool. In JDK's bin directory, Oracle has prepared a tool for us to analyze the bytecode of Class files: javap. We can view the bytecode contents of the class file with the javap-verbose command.
As you can see from our analysis, there are indeed 32 constants in the class file, from # 1 to # 32.
Summary of the structure of 17 data types
This is what the constant pool of Class files in the JVM virtual machine shared by Xiaobian is. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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.