Java class file internal structure parsing process detailed _java

Source: Internet
Author: User
Tags java se


When I was in college, I saw the second edition of the Java Virtual Machine specification, and recently reviewed the latest Java Virtual Machine specification third (Java SE 1.7) and found that the file structure of Java class in the Java Virtual Machine specification was not changed much, and it was sorted out by the way.
The Java language is Cross-platform, called once written, running everywhere. The reason is cross-platform, that is, Java defines a set of operating system, hardware-independent bytecode format, which is represented by the Java class file, Java class file inside the definition of the virtual machine can recognize the bytecode format, this format is platform-Independent, are consistent on Linux systems or on Windows systems. This is like HTML file, we define a good specification, this system just to follow the specification to show the contents of the inside. Like HTML is a class file, the browser is the same as the virtual machine, through the browser to perform the HTML rendering process, whether we use the mobile phone, Windows system, the Apple system on the Internet, the content displayed is the same. Java virtual machines can load predefined bytecode from a class file, or they can load bytecode from a network, database, or message file.
The Java class file structure is based on a byte stream, encoded in Unicode. In fact, think can also be fully expressed in XML file, but the byte code text in XML can be very large, occupy space, while parsing more time-consuming, and very easy to be artificially modified, resulting in unknown errors. The following is the internal structure of the class file

Copy Code code as follows:

Classfile {
U4 Magic;
U2 minor_version;
U2 major_version;
U2 Constant_pool_count;
Cp_info Constant_pool[constant_pool_count-1];
U2 access_flags;
U2 This_class;
U2 Super_class;
U2 Interfaces_count;
U2 Interfaces[interfaces_count];
U2 Fields_count;
Field_info Fields[fields_count];
U2 Methods_count;
Method_info Methods[methods_count];
U2 Attributes_count;
Attribute_info Attributes[attributes_count];
}



Briefly explain:

U4 represented by unsigned four bytes

U4 Magic: is a fixed value, the Java virtual machine called the magic number, mainly used to identify whether the Java virtual machine is supported by the file structure, is currently 0xCAFEBABE

U2 minor_version; U2 major_version; Represents the minor version number and the major version number

U2 Constant_pool_count; Cp_info Constant_pool[constant_pool_count-1]; This represents the number of constant pools and the constant pool information

U2 Access_flags: Represents the class access token, for example: public protected

U2 This_class: Represents the name of the class such as Java.lang.Object

U2 Super_class: Representing the parent class name

U2 Interfaces_count; U2 Interfaces[interfaces_count]; Implemented interface format and interface class name

U2 Fields_count; Field_info Fields[fields_count]; Number of fields and field information

U2 Methods_count; Method_info Methods[methods_count]; Number of methods and method information

U2 Attributes_count; Attribute_info Attributes[attributes_count]; Java class file internal attribute information, which has nothing to do with the properties defined by the Java language, is purely for Java virtual machines

All of the above are the class file internal structure information defined in the Java Virtual Machine specification, which can be referenced in Java Virtual machine specification (Java SE 7) Here's not a detail.

Each structure also defines its own structure information, such as: constant pool structure, field information, method information, class information, and each of these structures are referenced to each other, the following diagram briefly describes the internal structure information of Java class files

Where the information in the constant pool is the most complex, all information about the runtime of the Java Virtual machine is obtained from the constant pool, and the constant pool defines several structures

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.