In-depth understanding of the Java Class file format (1)

Source: Internet
Author: User

Position and Role of Class files in the Java Architecture


In the previous blog, I briefly explained the architecture and execution principles of Java virtual machines. This blog explains the formats of class files that can be recognized, loaded, and executed by JVM.


It is essential to understand JVM, Java language, and class file format. The reason is very simple. JVM does not understand the Java source file we write. We must compile the Java source file into a class file to be recognized by JVM. For JVM, the class file is equivalent to an interface, understanding this interface can help us better understand JVM behavior. On the other hand, the class file re-describes what we mean in the source file in another way, understanding how class files re-Describe the source files we have written is helpful for a deep understanding of the Java language and syntax. In addition, no matter what language, as long as it can be compiled into class files, it can be recognized and executed by JVM. Therefore, class files are not only the basis of cross-platform, but also the basis of JVM cross-language, understanding the class file format is helpful for us to learn other JVM-based languages.

In short, the class file is in the middle of the entire Java technical architecture, which plays an important role in understanding the entire system. :

Vc/J2bXExNq05r/release + 49sDgo6i78tXfvdO/release + release/release + DTprXEw + release + 6 jrCDL/release + release/uyv2 + 3ba809DL/release/b7dz + release /tbQ1by + response/tNf2Y2xhc3POxLz + yv2 + 3c/response + response/Cyv2 + 3c/response/qhtymjugo8YnI + response = "1" width = "600" cellspacing = "0" cellpadding = "1">TypeNameQuantityU4Magic1U2Minor_version1U2Major_version1U2Constant_pool_count1Cp_infoConstant_poolConstant_pool_count-1U2Access_flags1U2This_class1U2Super_class1U2Interfaces_count1U2InterfacesInterfaces_countU2Fields_count1Field_infoFieldsFields_countU2Methods_count1Method_infoMethodsMethods_countU2Attribute_count1Attribute_infoAttributesAttributes_count


Next, we will explain each item in the class file in detail.


Magic number and version number in the class file

(1) magic

The four bytes starting with the class file store the magic number of the class file. This magic number is a sign of the class file, and it is a fixed value: 0 XCAFEBABE. That is to say, it judges whether a file is a class-format file standard. If the first four bytes are not 0 XCAFEBABE, it indicates that it is not a class file and cannot be recognized by JVM.


(2) minor_version and major_version

The four bytes of the magic number are the version number and main version number of the class file. With the development of Java, the format of class files will also be changed accordingly. The version number indicates when the class file is added or changed. For example, the version numbers of the class files compiled by different versions of the javac compiler may be different, and the JVM versions of different versions may be different from the version numbers of the class files. Generally, the higher version of JVM can recognize the class files compiled by the lower version of javac compiler, while the lower version of JVM cannot recognize the class files compiled by the later version of javac compiler. If a later version of JVM is used to execute a later version of the class file, JVM will throw java. lang. UnsupportedClassVersionERror. The specific version changes will not be discussed here. Readers need to check their own materials.


Overview of constant pools in class files

In the class file, data items related to the constant pool are located after the version number. Constant pool is a very important data in the class file. The constant pool stores text strings, constant values, class names of the current class, field names, method names, descriptors of various fields and methods, and references to fields and methods of the current class, references to other classes in the current class. The constant pool contains almost all information about the class. Many other parts of the class file reference the data items in the constant pool, such as this_class, super_class, field_info, attribute_info, etc, in addition, the bytecode command also references the constant pool. This reference to the constant pool is used as an operand of the bytecode command. In addition, each item in the constant pool is also referenced.

The value of constant_pool_count in the class file is 1, indicating that each class has only one constant pool. The data in the constant pool is also one item, which is discharged in sequence without gaps. Each data item in the constant pool is accessed through an index, which is a bit similar to an array, except that the index of the first item in the constant pool is 1 rather than 0, if the constant pool item whose index is 0 is referenced elsewhere in the class file, it means that it does not reference any constant pool item. Each data item in the class file has its own type. In the same principle, each data item in the constant pool also has its own type. The types of data items in the constant pool are as follows:

Data item type in the constant pool Type flag Type description
CONSTANT_Utf8 1 Unicode string encoded by UTF-8
CONSTANT_Integer 3 Int-type Literal Value
CONSTANT_Float 4 Float Type Literal Value
CONSTANT_Long 5 Long Literal Value
CONSTANT_Double 6 Double Literal Value
CONSTANT_Class 7 Symbol reference for a class or interface
CONSTANT_String 8 String Literal Value
CONSTANT_Fieldref 9 Symbol reference for a field
CONSTANT_Methodref 10 Symbol reference for methods declared in a class
CONSTANT_InterfaceMethodref 11 Symbol reference for methods declared in an Interface
CONSTANT_NameAndType 12 References part of a field or Method


Each data item is called a XXX_info item. For example, a CONSTANT_Utf8 type item in a constant pool is a CONSTANT_Utf8_info item. In addition, each info item has a tag, which indicates the type of the info item in the constant pool. As shown in the table above, the tag value in a CONSTANT_Utf8_info is 1, while that in a CONSTANT_Fieldref_info is 9.

Java programs are dynamically linked. In the Implementation of Dynamic Links, the constant pool plays an important role. In addition to some literal values, the constant pool also contains the following symbol references:

(1) fully qualified names of classes and interfaces

(2) field name and Descriptor

(3) method name and Descriptor

Before explaining each data item in the constant pool in detail, we need to first understand the special strings in the class file, because a large number of special strings appear in the constant pool, these special strings are the fully qualified names and Descriptors mentioned above. To understand the data items in the constant pool, you must first understand these special strings.


The explanation of the class file will be continued in the subsequent blog posts.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.