The foundation of Java cross-platform

ByteCode is the program storage format used by VMS on different platforms and all platforms

The foundation of platform independence is also the foundation of language independence. The Java virtual machine does not include any Java

Language binding, which is associated only with the specific binary file format “Class file”, which is contained in the Class file

The Java virtual machine instruction set and symbol table and several other auxiliary information.

The nature of the Class

Any Class file corresponds to a unique Class or interface definition information, but on the other hand, the Class file is real

It does not necessarily exist as a disk file.

A Class file is a set of binary streams in 8-bit byte base units.

Class file format

The data items are arranged in a tightly packed Class file in strict order, without any delimiters

The entire Class file stores almost all the necessary data for the program to run, there is no gap.

The Class file format uses a pseudo-structure similar to the C-language constructs to store data, of which only two are

Data types: unsigned numbers and tables.

Unsigned numbers are basic data types. U1, U2, u4, and U8 represent 1 byte, 2 byte, and 4 respectively

Unsigned numbers of bytes and 8 bytes can be used to describe numbers, index references, quantity values, or in accordance with

Utf-8 encodings constitute string values.

A table is a compound data type consisting of multiple unsigned numbers or other tables as data items

“_info” at the end. Tables are used to describe hierarchical composite structures of data, and the entire Class file is essentially one

Table.

Class file format details

Class is not structured like a description language such as XML, because it does not have any delimiters, so the data items in it, none

In terms of order or quantity, it’s strictly defined, which byte means what, what length, what order

No change is allowed.

Including, in order:

Version of magic number and Class file

The first four bytes of each Class file are called the Magic Number, and their sole purpose is to identify the file

Whether the file is a Class file that can be accepted by the VM. The use of magic numbers rather than extensions for identification is primarily based on

Security concerns, as file extensions can be changed at will. The document format maker is free to choose the magic

Number, as long as the magic number is not widely used and does not cause confusion.

The next four bytes of the magic number store the version number of the Class file: the fifth and sixth bytes are the second version number

(MinorVersion), the 7th and 8th bytes are the Major Version numbers. The Java version number is from

For every major JDK release since JDK 1.1, the major version number increases by 1. Older JDK releases can go down

Compatible with a previous version of the Class file, but cannot run a later version of the Class file, even if the file format did not occur

The virtual machine must also refuse to execute Class files that exceed its version number for any changes.

Constant pool

The number of constants in the constant pool is not fixed, so in the constant pool entry need to place a u2 type of data, generation

Table Constant pool capacity count (constant_pool_count). Contrary to language conventions in Java, this allows

The quantity count starts at 1, not 0

There are two main types of constants in the constant pool: Literal and Symbolic References.

Literals are close to the Java language level concepts of constants, such as text strings, constant values declared final, and so on.

Symbolic references, on the other hand, are part of the compilation principle and include three types of constants:

The Fully Qualified Name, field Name and Descriptor, and square of the class and interface

Method name and descriptor

Access tokens

Access information that identifies some Class or interface level, including whether the Class is a Class or interface; Is it defined as

Public type; Whether to define an abstract type; If it is a class, whether it is declared final, etc

A collection of class indexes, parent indexes, and interface indexes

These three pieces of data determine the inheritance of the class. The class index is used to determine the fully qualified name of the class, and the superclass index is used for

Determines the fully qualified name of the parent of this class. Because the Java language does not allow multiple inheritance, there is only one parent index

All Java classes except java.lang.Object have a parent class, so all Java classes except java.lang.Object have a parent class

None of the Java classes has a parent index of 0. The interface index collection is used to describe which interfaces the class implements,

These implemented interfaces are implemented according to the IMPLEMENTS statement (extends if the class itself is an interface

The interfaces after the statement are ordered from left to right in the interface index collection

Set of field tables

Describes variables declared in an interface or class. Fields include class-level variables and instance-level variables.

What is the name of the field, what is the data type of the field defined, these are not fixed, can only refer to the constant pool

Is described by constants in.

Fields inherited from superclasses or parent interfaces are not listed in the field table collection, but native Java generations may be listed

Fields that do not exist in the code, such as the inner class, are automatically added to the external class in order to maintain access to the external class

Class instance field.

Method table collection

It describes the definition of a method, but the Java code in the method, after the compiler compiles into bytecode instructions, stored

In a property called “Code” in the property sheet collection.

Similar to the field table collection, if the parent method has not been overridden in a subclass, the method table collection

Method information from the parent class does not appear. But again, it is possible to have methods that are automatically added by the compiler

Typical examples are the class constructor “< clinit >” method and instance constructor “< init >”

Property sheet collection

Store Class files, field tables, and method tables with their own set of property tables to describe certain scenarios

Proprietary information. The Code for methods, for example, is stored in the Code property sheet.

Bytecode instruction

Java virtual machine instructions consist of a one-byte number (called an opcode) that represents the meaning of a particular operation,

Opcode) followed by zero representing at most the parameters required for the operation (called Operands)

Into. Since the Java virtual machine opcodes are limited to one byte in length (that is, 0 to 255), this implies the operation of the instruction set

The total number of codes cannot exceed 256.

Most instructions contain information about the data type to which they operate. Such as:

The ILoAD directive loads int data from a local variable table into the operand stack, whereas the FLOAD directive loads

Data of type float.

Most directives do not support the integer types byte, char, and short, or even Boolean

Type. Most operations on Boolean, byte, short, and CHAR data actually use corresponding

Int as the operation type

Read bytecode as a basic skill to learn about Java VIRTUAL machines (VMS). Please be familiar with and master the common instructions

Can be.

Load and store instructions

Instructions for transferring data back and forth between a local table of variables in a stack frame and the operand stack include the following.

Load a local variable onto the action stack: ILoad, ILoAD_ < N >, lload, lload_ < N >, fload, fload_

< N >, dload, dload_ < N >, aload, aload_ < N >.

Store a value from the operand stack to the local variable table: istore, istore_ < n >, lstore, lstore_ < n >,

Fstore, fstore_ < N >, dstore, dstore_ < N >, astore, astore_ < N >.

Add a constant to the operand stack: bipush, sipush, LDC, LDc_w, LDC2_W, aconST_NULL,

Iconst_m1, iconst_ < I >, lCONST_ < L >, fCONST_ < f >, dconst_ < d >.

Instruction that extends the access index of a local variable table: wide.

An operation or arithmetic instruction

Used to perform a specific operation on two operand stacks and store the result back to the top of the stack.

Add instructions: iadd, ladd, fadd, dadd.

Subtraction instructions: ISub, LSUB, fsub, dsub.

Multiplication instructions: IMul, LMUl, FMUl, dMUl, etc

Type conversion instruction

You can convert two different numeric types to each other,

The Java VIRTUAL machine directly supports wide type conversions (that is, safe conversions from a small type to a large type) of the following numeric types

Change) :

Int to long, float, or double.

Long to float, double.

Float to double.

In playing a role of Narrowing Numeric Conversions, conversion instructions must be explicitly used

These conversion instructions include: I2B, I2C, I2S, L2I, F2I, F2L, D2I, D2L and D2F.

Directives for creating class instances:

New.

Instructions for creating arrays:

Newarray, AneWarray, multianewarray.

Access field instructions:

Getfield, putfield, getStatic, putStatic.

Array access related instructions

The instruction to load an array element onto the operand stack: baload, caload, Saload, iaload, laload,

Faload, daload, aaload.

Instructions to store the values of an operand stack in an array element: Bastore, Castore, sastore, iastore,

Fastore, Dastore, aastore.

The instruction to take the length of an array: arrayLength.

Directives to check class instance types:

Instanceof, checkcast.

Operand stack management instructions

The Java virtual machine provides some tools for manipulating operand stacks directly, just as it would in a normal data structure

To remove one or two elements from the top of the operand stack: pop, pop2.

Duplicates one or two values and pushes them back to the top: dUP, dup2,

Dup_x1, dup2_x1, dup_x2, dup2_x2.

Swap the top two values of the stack: swap.

Control transfer instruction

Control transfer instructions allow the Java VIRTUAL machine to conditionally or unconditionally instruct from a specified location rather than control transfer fingers

Let the next instruction continue to execute the program, from the conceptual model understanding, can be considered control transfer instruction is conditional

Or modify the value of the PC register unconditionally. The control transfer instructions are as follows.

Conditional branches: IFEQ, IFLT, IFLE, IFNE, IFGT, IFGE, IFNULL, IFnonNULL, IF_ICMPEQ, IF_ICMPNE,

If_icmplt, IF_ICMPGT, IF_ICmPLE, IF_ICMPGE, IF_ACMPEq, and if_ACMPne.

Compound condition branches: Tableswitch and LookupSwitch.

Unconditional branches: GOTO, GOTO_W, JSR, jSR_W, ret.

Method call instruction

The Invokevirtual directive is used to invoke instance methods of objects, dispatching them based on the actual type of the object (virtual method partitioning)

Pie), which is also the most common method dispatch in the Java language.

The InvokeInterface directive is used to invoke an interface method, which searches at run time for a pair that implements the interface method

For example, find the appropriate method to call.

The Invokespecial command is used to call some instance methods that require special processing, including instance initialization methods and private parties

Method and superclass methods.

The Invokestatic directive is used to invoke class methods (static methods).

The InvokeDynamic directive is used to dynamically resolve the method referenced by the call point qualifier at run time and execute that method

The dispatch logic of the previous four invokedynamic instructions is solidified inside the Java VIRTUAL machine

The dispatch logic is determined by the bootstrap method set by the user.

Method call instructions are independent of the data type.

Method return instruction

Is distinguished by the type of return value, including iReturn (when the return value is Boolean, byte, char, short, and int)

Type), lreturn, freturn, dreturn, and areturn, as well as areturn directive to declare as

Void methods, instance initializer methods, and class initializer methods for classes and interfaces.