In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to understand the constant pool of Java Class files, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
The existence of Class files enables programs written in different languages to run on the Java virtual machine. As long as the Class files compiled by these languages conform to the specifications defined by the Java virtual machine, the Java virtual machine can load and execute these Class files. As shown in the following figure:
The Class file is a group of binary streams based on 8-bit bytes. Each data item is arranged in the Class file in strict order. When the data that needs to occupy more than 8 bytes is encountered, it is divided into several 8-bit bytes according to the large end order.
There are only two data types in the Class file:
Unsigned table
Unsigned numbers belong to the basic data type. U1, U2, U4, and U8 represent 1, 2, 4, and 8 bytes of unsigned numbers, respectively. Unsigned numbers can be used to describe numbers, index references, quantitative values, or string values encoded by UTF-8.
A table is a data structure consisting of unsigned numbers and other tables, all of which end with _ info.
When describing a variable-length type data, it is usually in the format of a capacity counter and several data items.
Public class ClassTest {
Private int m
Private int inc () {return m + 1;}}
Next we will use the above code to analyze the contents of the Class file.
The picture above is from the compiled Class file.
Magic number (magic) and version number
The four bytes fixed at the beginning of the Class file are magic numbers, and the only purpose is to determine whether the file is a class file that can be received by the virtual machine. The magic number of Java is fixed as 0xCAFEBABE, the next two bytes represent the minor version number, the next two bytes are the major version number, the major version number is 0x0034 (decimal is 52, that is, JDK1.8), the high version of JDK can run previous versions of Class files, but can not run later versions of Class files, the popular point will be downward compatibility.
Constant pool
After the magic number and version, then is the most critical part of the constant pool in the Class file, constant pool consists of a counting pool and specific constant items, in our code, the number of constant pool is 0x0013 (decimal is 19), on constant pool there is a relatively special place is that he starts counting from 1, that is to say, if we count pool value is 19, in fact there are only 18 constant items.
There are two main types of constants stored in the constant pool:
Literal quantity symbol reference
Literals mainly refer to text strings, constant values declared as final, etc.
Symbolic references contain three main types of constants:
Fully qualified names of classes and interfaces, field names and descriptor method names and descriptors
When there is no connection when the Java code is compiled, that is, the final memory layout information of each method and field will not be saved in the Class file, and the symbolic references of these fields and methods will not get the real memory entry address if they are not converted at run time, so when the virtual machine is running, it is necessary to obtain the corresponding symbol references from the constant pool, parse and translate them to the specific memory address when the class is created and run.
At present, there are mainly the following table items in the constant pool:
These tables all have a fixed field, that is, the first byte is a flag bit that represents the corresponding type, as shown in the figure above.
You can list the contents of the file bytecode through javap-verbose xxx.class
By comparing the index of items in the javap-verbose constant pool with the binary representation of our class file, we can find that there is an one-to-one correspondence.
On how to understand the Java Class file constant pool to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.