Java Virtual Machine (i)-java class file detailed __java

Source: Internet
Author: User
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, in Linux systems are all the same on the Windows system. 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.

Today's Java virtual machines have been called a common platform, not only to run the Java language, Groovy,jruby,jython, and so on a large number of dynamic languages can also be directly on the Java Virtual machine to run, The principle is that these dynamic language compilers compile source files into the same bytecode files as Java, so that Java virtual machines can execute these dynamic languages as they do in the Java language.

The byte code class class file is composed of a series of byte code commands that represent the semantics of various constants, variables, keywords, and operational symbols in the program, and so on.

Java class file is a group of 8 bytes of binary flow, the data items in strict order in the class class file, the middle without adding any separator,

When a data item that requires more than 8 bytes of space is encountered, it is divided into several 8-bit bytes to store in the previous form of the high position.

The Java Virtual machine stipulates that class file formats are stored in a pseudo structure similar to the C language structure, which has only two data types: unsigned numbers and tables:

(1). Unsigned number:

Data belonging to the basic type, U1, U2, U4, U8 to represent 1 bytes, 2 bytes, 4 bytes, and 8 bytes of unsigned numbers, unsigned numbers can be used to describe numbers, index references, quantity values, or string values encoded by UTF-8.

(2). Table:

A composite data type consisting of more than one unsigned number or other table as a data item, so the tables habitually end with "_info." Tables are used to describe hierarchical relational composite structure data, and the entire class file is essentially a table.

The Java class class file structure is as follows:

type

name

Quantity

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

Attributes_count

1

Attribute_info

Attributes

Attributes_count

Class class files do not have any delimiters, are in strict accordance with this structure table order, the following specific description of the meaning of each name:

(1). Magic:

The first 4 bytes of each class file are called Magic Numbers, and its only function is to determine whether the file is a class file that can be received by a Java virtual machine, that is, a Java class file that determines whether a file is compliant. Although the suffix name ". Class" can indicate that the file is a class file, but the file suffix name can be arbitrarily changed, based on security considerations, many files through the magic value to uniquely determine the file type, Java class file magic number is: 0xCAFEBABE.

(2). Minor_version and Major_version:

The 5th and 6th bytes of each class file represent the minor version number of the class file, and the 7th and 8th bytes represent the major version number of the class file. The primary and minor version number of the class file is determined by the JDK, and jdk1.0~jdk1.1 uses the 45.0~45.3 version number (45 is the main version is good, dot.) After the minor version number, starting with JDK1.1, each major version of the JDK major version number plus 1.

When a class master and minor version number is used, a high version of the Java Virtual Machine can be forward compatible, running a class bytecode file compiled by a lower version of JDK, and a lower version of the Java Virtual Machine cannot run a class bytecode file compiled by a high version of JDK. When a lower version of a Java Virtual machine runs a class bytecode file compiled by a high version of JDK, it usually reports an exception that resembles the following:

1. Exception in thread "main" java.lang.unsupportedclassversionerror:a (Unsupporte
2. D Major.minor version 49.0)

jdk1.0~jdk1.1 uses the 45.0~45.3 version number, JDK1.2 uses the 46.0~46.65535 version number, JDK1.3 uses the 47.0~47.65535 version number, JDK1.4 uses the 48.0~48.65535 version number , JDK1.5 uses the 49.0~49.65535 version number, JDK1.6 uses the 50.0~50.65535 version number, JDK1.7 uses the 51.0~51.65535 version number.

The major version number can be changed at compile time by specifying the-target parameter, such as JDK1.6 if the target parameter is not given at compile time, the main version number of the compiled class file is 50, if the "-target 1.4-source 1.4" argument is given, The main version will become 48, and if the "-target 1.5" argument is given, the main version will become 49.

(3). Constant_pool_count and Constant_pool:

Constant_pool_count represents the number of constant pools in the class file, because the count of the constant pool starts at 1, so the capacity of the constant pool is constant_pool_count-1.

The No. 0 constant is empty for special consideration, in order to satisfy some of the index values pointing to the constant pool in some particular cases, the expression "does not point to any constant pool" is required.

The Constant_pool constant pool is the first table type data that appears in the class file, and the constant pool holds two main class constants:

A. Literal (Literal): Includes text strings, final type constant values.

B. Symbolic reference (Symbolicreferences): includes the fully qualified name of the class and interface, the name and descriptor of the field, and the name and descriptor of the method.

(4). Access_flags: An access flag used to represent class or interface levels, that is, access control information at the level of classes or interfaces, usually storing information such as: Class class files are classes, interfaces, enumerations, or annotations; are defined as public types ; Whether it is defined as an abstract type, whether the class is final, and so on.

(5). The This_class, Super_class, and Interfaces:this_class class indexes are used to determine the fully qualified name of the class, super_class the parent class index to determine the fully qualified name of the parent class, The interfaces interface index is used to determine the fully qualified name of an interface, and because multiple interfaces can be implemented in Java, Interfaces_count is used to store the number of interfaces.

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.