Home> Java> javaTutorial> body text

Detailed graphic and text explanation of class file format in Java

黄舟
Release: 2017-06-18 09:45:32
Original
1787 people have browsed it

For understanding the JVM and in-depth understanding of the Java language, learning and understanding the format of the class file is a must-have homework

The position and role of the Class file in the Java architecture

For understanding the JVM and in-depth understanding of the Java language, learning and understanding the format of class files are all tasks that must be mastered. The reason is very simple. The JVM will not understand the Java source files we write. We must compile the Java source files into class files before they can be recognized by the JVM. For the JVM, the class file is equivalent to an interface. Understanding this interface can help We better understand the behavior of the JVM; on the other hand, the class file re-describes the meaning we want to express in the source file in another way. Understanding how the class file re-describes the source file we wrote, for an in-depth understanding of the Java language and grammar are very helpful. In addition, no matter what language it is, as long as it can be compiled into a class file, it can be recognized and executed by the JVM. Therefore, the class file is not only the basis of cross-platform, but also the basis of JVM cross-language. Understanding the class file format is essential for us to learn based on JVM of other languages would be of great help.

In short, in the entire Java technology architecture, class files are in the middle and play a connecting role in understanding the entire system. As shown in the figure:

Class file format overview

class file is a An 8-bit binary stream file, each data item is arranged in a tight order from front to back, with no gaps between adjacent items. This makes the class file very compact, lightweight, and can be quickly loaded into memory by the JVM. And takes up less memory space. After our Java source files are compiled, each class (or interface) occupies a separate class file, and all information in the class will be described accordingly in the class file. Because class files are very flexible, they are even more flexible than Java source files have stronger description capabilities.

The information in the class file is arranged one by one. Each piece of data has its fixed length. Some occupies one byte, some occupies two bytes, and some occupies four. byte or 8 bytes, the different lengths of the data items are represented by u1, u2, u4, u8 respectively, which respectively indicate that a data item occupies one byte, two bytes, 4 bytes and 8 in the class file. bytes. You can think of u1, u2, u3, and u4 as the "types" of class file data items. The following data items exist in the

class file (this chart is referenced from "In-depth Java Virtual Machine"):

magic

type name

Quantity

##u4

1

u2

minor_version

1

u2

major_version

1

u2

constant_pool_count

1

cp_info

constant_pool

constant_pool_count - 1

u2

access_flags

1

u2

this_class

1

u2

super_class

1

u2

interfaces_count

1

u2

interfaces

interfaces_count

u2

fields_count

1

field_info

fields

fields_count

u2

methods_count

1

method_info

methods

methods_count

u2

attribute_count

1

attribute_info

attributes

attributes_count


The following is a detailed explanation of each item in the class file.

The magic number and version number in the class file

(1) magic

is at the beginning of the class file The four bytes store the magic number of the class file. This magic number is the symbol of the class file. It is a fixed value: 0XCAFEBABE. In other words, it is the standard for judging whether a file is in class format. If the first four bytes are not 0XCAFEBABE, it means that it is not a class file and cannot be recognized by the JVM.

(2) minor_version and major_version

The four bytes following the magic number are the version number and major version number of the class file. As Java develops, the format of class files will also undergo corresponding changes. The version number indicates when the class file was added or changed. For example, the version numbers of class files compiled by different versions of javac compilers may be different, and the version numbers of class files recognized by different versions of JVM may also be different. In general, a higher version of the JVM can recognize a lower version of the class file. The class files compiled by the javac compiler, but the lower version of the JVM cannot recognize the class files compiled by the higher version of the javac compiler. If a lower version of the JVM is used to execute a higher version of the class file, the JVM will throw java.lang.UnsupportedClassVersionError. The specific version number changes will not be discussed here, and readers in need can consult the information by themselves.

Constants in the class filePool overview

In the class file, the constant pool is located after the version number related data items. The constant pool is a very important piece of data in the class file. The constant pool stores textstring, constant value, class name of the current class, field name, method name, descriptors of each field and method, reference information to the fields and methods of the current class, current Reference information to other classes in the class, etc. The constant pool contains descriptions of almost all the information in the class. Many other parts in the class file are references to data items in the constant pool, such as this_class, super_class, field_info, attribute_info, etc. to be discussed later. In addition, bytes There is also a reference to the constant pool in the code instruction. This reference to the constant pool is used as an operand of the bytecode instruction. In addition, each item in the constant pool will also refer to each other.

The value of the item constant_pool_count in the class file is 1, indicating that each class has only one constant pool. The data in the constant pool are also discharged one by one without gaps. Each data item in the constant pool is accessed throughindex, which is somewhat similar to an array, except that the index of the first item in the constant pool is 1, not 0. If it is referenced elsewhere in the class file A constant pool item with index 0 means that it does not reference any constant pool item. Each data item in the class file has its own type. In the same way, each data item in the constant pool also has its own type. The types of data items in the constant pool are as follows:

##3 CONSTANT_Float CONSTANT_Long CONSTANT_Double CONSTANT_Class CONSTANT_String CONSTANT_Fieldref CONSTANT_Methodref CONSTANT_InterfaceMethodref CONSTANT_NameAndType

Types of data items in the constant pool

Type flag

Type description

##CONSTANT_Utf8

1

UTF-8 encoded Unicode string

CONSTANT_

Integer

##int type literal

4

##float type literal value

5

long type literal

6

double type literal

7

A symbolic reference to a class or interface

8

String type literal value

9

A symbolic reference to a field

10

A symbolic reference to a method declared in a class

11

A symbolic reference to a method declared in an interface

12

Partial symbolic reference to a field or method

Each data item is called a XXX_info item. For example, an item of type CONSTANT_Utf8 in a constant pool is a CONSTANT_Utf8_info. In addition, each info item has a flag value (tag). This flag value indicates the type of the info item in this constant pool, as can be seen from theTableabove. , the tag value in a CONSTANT_Utf8_info is 1, and the tag value in a CONSTANT_Fieldref_info is 9.

Java programs are dynamically linked. In the implementation of dynamic linking, the constant pool plays a pivotal role. In addition to storing some literals, the constant pool also stores the following symbolic references:

(1) The fully qualified names of classes and interfaces

(2 ) Field names and descriptors

(3) Method names and descriptors

Before explaining each data item in the constant pool in detail, we need to first Learn about thespecial charactersstrings in the class file, because in the constant pool, special strings appear in large numbers. These special strings are the fully qualified names and descriptors mentioned above. To understand each data item in the constant pool, you must first understand these special strings.

The above is the detailed content of Detailed graphic and text explanation of class file format in Java. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!