Java class file Structure notes

Source: Internet
Author: User

We all know that the Java implementation of the cross-platform is virtual machine technology, the source files are compiled into an operating system-independent, only the virtual function to identify and execute the bytecode files, by the various operating systems on the JVM to be responsible for execution, shielding the underlying specific operating system. Here we come to know the true look of this bytecode file that only the JVM knows.

In order to save space, the class file does not have any separators, each data item is a compact arrangement next to each other, so whether the order or quantity is strictly defined, which byte represents what meaning, the length is how many, the order, is not allowed to change. Let's take a look at the overall structure of the class file:

class file Structure

Where constants, interfaces, fields, methods, and properties are tightly arranged in their respective structures, the number is determined by the number field in front of them. The smallest unit in a class file is 1 bytes, and more than one byte of data is stored in the big-endian way.

Each of these sections is described in turn:

Magic number

The magic number is used to determine whether the type of file is a class file, because it is not reliable to determine file types by file extension only.

This magic number accounts for the beginning of the file 4 bytes, for CA FE BA be. (Note: The literal representation here is the hexadecimal number, not the ASCII code)

Version number

The next 4 bytes are the class file version number, where the first two bytes represent the minor version number, and the last two bytes represent the major version number (starting at 45).

Virtual machines can be backwards-compatible to run class files, but cannot run higher than their version of class files.

Constant pool

Because the number of constants in a constant pool is indeterminate, the entry for the constant pool requires two bytes to represent the constant pool capacity count value (constant pool index starting from 1).

There are 14 constant types, each with its own corresponding structure, but the beginning of a byte also represents the flag bit, which is used to distinguish between different types.

The following are the specific types of 14 constants and their corresponding flag bits:

The structure of each type is as follows (where U1 represents 1 bytes, U2 represents 2 bytes, and so on):

Read the constant pool first read the flag bit, judge the constant type, you can know the corresponding structure, to obtain the corresponding information.

Access flags

The two bytes after Chang represents the access flag, which is the class or interface, whether it is public, and so on. Different meanings have different flag values (the unused markers are all 0.) ), the specific information is as follows:

Class Index

The class index is two bytes, pointing to constants of type Constant_class_info in the constant pool, and the constant structure of this type is shown in the constant pool chart, which contains an index to the fully qualified name constant entry.

Parent Class Index

Because Java only allows single inheritance, there is only one parent class, the content of which is the same as the class index.

Java class file Structure notes

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.