[Java] Deep understanding of Java class file format (i) __java zone

Source: Internet
Author: User
The location and role of class files in the Java architecture


In the previous blog, the Architecture and implementation principles of Java Virtual machines were roughly explained. This blog mainly explains the format of class files that can be identified by the JVM, loaded and executed.


Learning and understanding the format of a class file is a lesson that must be mastered for understanding the JVM and understanding the Java language in depth. The reason is simple, the JVM does not understand our Java source files, we have to compile the Java source file into a class file to be recognized by the JVM, for the JVM, the class file is equivalent to an interface, understanding the interface, can help us better understand the behavior of the JVM, on the other hand, The class file, in a different way, describes what we want to say in the source file, and it is helpful to understand the Java language and grammar by understanding how the class file can be used to rewrite the source files we write. In addition, no matter what language, as long as can be compiled into a class file, can be recognized and executed by the JVM, so the class file is not only a cross-platform foundation, but also the JVM's cross-language basis, understanding the class file format, for us to learn other languages based on the JVM will be helpful.


In short, in the entire Java technology architecture, class files in the middle of the position, to understand the entire system has a connecting link role. As shown in the figure:



class file format Overview
Class file is a 8-byte binary stream file, each data item in order close to the back of the past, there is no gap between the adjacent items, so that the class file is very compact, lightweight, the JVM can be quickly loaded into memory, and occupy a small amount of memory space. Our Java source files, after being compiled, each class (or interface) occupies a single class file, and all the information in the class file in a corresponding description, because the class file is flexible, it even more than the Java source file has a stronger description of the ability.
The information in a class file is arranged in one item, each piece of data has its fixed length, some accounting for a byte, some two bytes, some four bytes or 8 bytes, the different lengths of the data items with U1, U2, U4, U8 said, Represents a data item that occupies a byte, two bytes, 4 bytes, and 8 bytes in the class file. You can put U1, U2, U3, U4 as the "type" of the class file data item.
The following data items exist in the class file (the chart is referenced from the Deep Java Virtual machine):

Type Name Number
U4 Magic 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.


magic number and version number in class file

(1) Magic

At the beginning of the class file four bytes, storing the magic number of class files, this magic number is a class file symbol, he is a fixed value: 0XCAFEBABE. That is, he is the standard for determining whether a file is a class format, and if the first four bytes are not 0XCAFEBABE, then it is not a class file and cannot be recognized by the JVM.


(2) Minor_version and Major_version

The next four bytes of the magic number are this version number and major version number of the class file. With the development of Java, the format of the class file will be changed accordingly. The version number indicates when the class file was added or changed. For example, different versions of the Javac compiler compile the class file, the version number may be different, and different versions of the JVM can recognize the version number of the class file may also be different, in general, the high version of the JVM can identify the lower version of the Javac compiler compiled class files, The lower version of the JVM does not recognize the high version of the Javac compiler's compiled class file. If you use a lower version of the JVM to execute a high version of the class file, the JVM throws a java.lang.UnsupportedClassVersionError. Specific version number changes are no longer discussed here, and readers need to consult their own data.


constant Pool overview In a class file

In the class file, the constant pool-related data item is behind the version number. Chang is a very important piece of data in the class file. The constant pool holds literal strings, constant values, the class name of the current class, the field name, the method name, the descriptors for each field and method, the reference information for the fields and methods of the current class, the reference information for other classes in the current class, and so on. A constant pool contains descriptions of almost all the information in a class, and many other parts of the class file are references to data items in a constant pool, such as This_class, Super_class, Field_info, Attribute_info, etc.  There is also a reference to a constant pool in a byte code directive, which is a reference to a constant pool as an operand of a byte-code instruction. In addition, each item in a constant pool is referenced to each other.


The value of the item Constant_pool_count in the class file is 1, which means that each class has only one constant pool. The data in a constant pool is also one item, and there is no sequential emission of gaps. Each data item in a constant pool is accessed by index, somewhat similar to an array, except that the first item in the constant pool has an index of 1, not 0, and if another place in the class file references a constant pool entry with an index of 0, it does not refer to any of the constant pool entries. Each data item in a class file has its own type, and in the same way, each data item in a constant pool has its own type. The types of data items in a constant pool are the following tables:

Data item types in a constant pool Type flags Type description
Constant_utf8 1 UTF-8 encoded Unicode string
Constant_integer 3 int type literal
Constant_float 4 Float type literal
Constant_long 5 Type long literal
Constant_double 6 Type double literal
Constant_class 7 A symbolic reference to a class or interface
Constant_string 8 String type literal
Constant_fieldref 9 A symbolic reference to a field
Constant_methodref 10 A symbolic reference to a method declared in a class
Constant_interfacemethodref 11 A symbolic reference to a method declared by a port
Constant_nameandtype 12 A partial symbolic reference to a field or method


Each data item is called a Xxx_info item, for example, a Constant_utf8 type item in a constant pool, which is a constant_utf8_info. In addition, each info item has a flag value (tag), which indicates what the type of the info item in this constant pool is, and from the table above it can be seen that the tag value in a constant_utf8_info is 1 and a constant_ The tag value in the Fieldref_info is 9.

Java programs are dynamically linked, and in the implementation of dynamic links, constant pool players play a pivotal role. In addition to storing some literal amounts, there are several symbolic references in the constant pool:

(1) Fully qualified name of class and interface

(2) The name and descriptor of the field

(3) The name and descriptor of the method

Before we go into the details of the individual data items in a constant pool, it is necessary to look at the special strings in the class file, because in a constant pool, a large number of special strings appear, and these special strings are the fully qualified names and descriptors mentioned above. To understand the individual data items in a constant pool, you must first understand these special strings.


For class file explanation will continue in the follow-up posting, welcome attention.


===========================================================================================

Original: http://blog.csdn.net/zhangjg_blog/article/details/21486985

===========================================================================================



Related Article

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.