Java Virtual Machine

Source: Internet
Author: User

What is a Java Virtual Machine?
Java Virtual Machine (JVM)
A Java virtual machine is an imaginary machine that is simulated by software on an actual computer. The Java Virtual Machine has its own hardware, such as the processor, stack, and register. It also has the corresponding command system.

Features of Java Virtual Machine
A very important feature of Java is its independence from the platform. The use of Java virtual machines is the key to achieving this feature. General advanced languages must at least compile different target codes to run on different platforms. After the Java Virtual Machine is introduced, the Java language does not need to be re-compiled when running on different platforms. Java language usage mode: the Java Virtual Machine shields information related to specific platforms, so that the Java language compiler only needs to generate the target code (bytecode) that runs on the Java Virtual Machine ), it can be run on multiple platforms without modification. When executing the bytecode, the Java Virtual Machine interprets the bytecode as a machine instruction execution on a specific platform.

Principal used by the Java Virtual Machine
Java Virtual Machine (VM) is the underlying implementation basis of the Java language. Anyone interested in the Java language should have a general understanding of the Java Virtual Machine. This helps you understand the nature of Java and the use of Java. For software developers who want to implement Java virtual machines on a specific platform, compiler authors of Java language and those who want to implement Java virtual machines using hardware chips, they must have a deep understanding of the specifications of Java virtual machines. In addition, if you want to expand the Java language or compile other languages into the bytecode of the Java language, you also need to have a deep understanding of the Java Virtual Machine.

Data Types supported by Java Virtual Machine
Java Virtual Machine supports the following basic data types:
Byte: // complement of a signed integer in 1 byte
Short: // 2byte signed integer Complement
Int: // 4-byte signed integer Complement
Long: // 8-byte signed integer Complement
Float: // 4-byte IEEE754 Single-precision floating point number
Double: // 8-byte IEEE754 double-precision floating point number
Char: // 2-byte unsigned Unicode Character

Almost all Java type checks are completed at compilation. The raw data types listed above do not need to be marked with hardware during Java execution. The bytecode (Instruction) for operating these raw data types has already pointed out the data type of the operands. For example, the iadd, ladd, fadd, and dadd commands add two numbers, the operands are int, long, float, and double. The VM does not set separate commands for the boolean type. Boolean data is processed by integer commands, including integer return. Boolean arrays are processed using byte arrays. The VM uses a floating point number in IEEE754 format. Older computers in IEEE format are not supported. It may be very slow when running Java numeric computing programs.

Other data types supported by virtual machines include:
Object // 4-byte reference to a Javaobject
ReturnAddress // 4 bytes for the jsr/ret/jsr-w/ret-w command
Note: Java arrays are processed as objects.

The specifications of virtual machines have no special requirements on the internal structure of objects. In Sun's implementation, an object reference is a handle, which contains a pair of pointers: one pointer points to the object's method table, and the other points to the object's data. Programs represented by bytecode of Java virtual machines should comply with Type rules. Java Virtual Machine implementation should reject the execution of bytecode programs that violate the type regulations. The Java Virtual Machine can only run on 32-bit address space machines due to the bytecode definition restrictions. However, you can create a Java virtual machine that automatically converts bytecode into a 64-bit format. From the data types supported by Java virtual machines, we can see that Java strictly defines the internal formats of data types, so that the implementation of various Java virtual machines has the same interpretation of data, this ensures that Java is platform independent and

Portability.
Java virtual machine architecture
A Java virtual machine consists of five parts: a set of instruction sets, a set of registers, a stack, a useless Unit collection heap (Garbage-collected-heap), and a method area. These five components are the logical components of Java virtual machines and do not rely on any implementation technology or organization method, but their functions must be implemented in some way on real machines.

1. Java Instruction Set
The Java Virtual Machine supports approximately 248 bytecode. Each bytecode performs a basic CPU operation, for example, adding an integer to a register or subroutine transfer. The Java instruction set is equivalent to the Java program assembly language.
The commands in the Java Instruction Set contain a single-byte operator, which is used to specify the operation to be executed. There are also 0 or multiple operands to provide the parameters or data required for the operation. Many Commands have no operands and are composed of only one single-byte operator.
The execution process of the internal loop of the virtual machine is as follows:
Do {
Obtains an operator byte;
Execute an action based on the operator value;
} While (the program has not ended)

Because of the simplicity of the command system, the virtual machine execution process is very simple, which is conducive to improving the execution efficiency. The number and size of operands in the command are determined by the operator. If the operand is larger than a byte, the storage order of the operand is higher than that of the byte. For example, a 16-bit parameter occupies two bytes and its value is:

The first byte * 256 the second byte code command stream is generally only byte aligned. The command tabltch and lookup are exceptions. The two commands require forced 4-byte boundary alignment.

2. Registers
The registers of the Java Virtual Machine are used to save the running status of the machine, similar to some special registers in the microprocessor.
Java Virtual Machine registers have four types:
Pc: Java program counter.
Optop: pointer to the top of the operand stack.
Frame: pointer to the execution environment of the current execution method.
Vars: pointer to the first variable in the local variable area of the current execution method.

3. Stack
The stack of a Java Virtual Machine has three areas: local variable zone, Runtime Environment zone, and operand zone.
(1) Each Java method in the local variable area uses a fixed local variable set. They are addressing according to the word offset from the vars register. Local variables are all 32-bit. Long integers and double-precision floating-point numbers occupy the space of two local variables, but are addressed according to the index of the first local variable. (For example, if a local variable with index n is a double-precision floating point number, it actually occupies the storage space represented by index n and n 1 .) The virtual machine specification does not require the 64-bit values in local variables to be 64-bit aligned. The virtual machine provides commands to load the values in local variables to the operand stack, and also to write the values in the operand stack into the local variables.

(2) Information contained in the runtime environment is used for Dynamic Links, normal method return, and Exception Propagation.

· Dynamic Link
The runtime environment includes pointers to the interpreter symbol table pointing to the current class and current method, which is used to support dynamic links of method code. The class file code of the method uses the symbol when referencing the method to be called and the variable to be accessed. Dynamic links translate symbolic method calls into actual method calls, and load necessary classes to explain symbols that have not yet been defined, translate variable access into an offset address corresponding to the storage structure of these variables during runtime. Dynamic Linking of methods and variables makes changes to other classes used in methods do not affect the code of this program.

· Normal method return
If the current method ends normally, a returned command with the correct type will be returned when the called method is executed. The execution environment is used to restore the caller's register when the caller returns normally, and adds an appropriate value to the caller's program counter to skip the executed method call command, then, the invocation continues in the caller's execution environment.

· Exception and Error Propagation
An Exception is called an Error or Exception in Java. It is a subclass of the Throwable class. The cause in the program is: ① Dynamic Link Error, if you cannot find the required class file. ② Runtime error, such as a reference to a null pointer

· The program uses the throw statement.
When an exception occurs, the Java Virtual Machine takes the following measures:

· Check the catch clause table associated with the current method. Each catch clause contains its valid instruction range, exception types that can be processed, and the address of code blocks that can handle exceptions.

· The catch clause that matches the exception should meet the following conditions: the exception-causing command is within the scope of its command, and the exception type is the child type of the exception type that it can handle. If a matched catch clause is found, the system transfers it to the specified exception processing block for execution. If no exception processing block is found, repeat the process of searching for a matched catch clause, until all nested catch clauses of the current method are checked.

· As the VM continues executing from the first matched catch clause, the order in the catch clause table is very important. Because Java code is structured, you can always sort all the exception processors of a method into a table in order to view the values of any possible program counters, you can find appropriate Exception Processing blocks in a linear order to handle exceptions under the program counter value.

· If no matching catch clause can be found, the current method will get a result of "no exceptions intercepted" and return it to the caller of the current method, as if an exception had just occurred in the caller. If no exception handling block is found in the caller, the error will continue. If an error is propagated to the top layer, the system calls a default Exception Handling block.

(3) In the operations stack, machine commands only take the operands from the operations stack, operate on them, and return the results to the stack. The reason for choosing the stack structure is that the virtual machine behavior can be efficiently simulated on machines with only a few registers or non-General registers (such as intelease. The operand stack is 32-bit. It is used to pass parameters to a method and receive results from the method. It is also used to support operation parameters and save the operation results. For example, the iadd command adds two integers. The two integers added should be the two characters at the top of the operand stack. These two words are pushed into the stack by the previous commands. The two integers will pop up and add from the stack, and press the result back to the operand stack.

Each raw data type has a special command to perform required operations on them. Each operand needs a storage location in the stack, except for the long and double types, they need two locations. An operand can only be operated by an operator of its type. For example, it is invalid to press the numbers of two int types if they are treated as a long number. In Sun's Virtual Machine implementation, this restriction is enforced by the bytecode validators. However, there are a few operations (operators dupe and swap) that are used to perform operations on the runtime data zone regardless of the type.

4. Useless Unit collection heap
The Java heap is a runtime data zone, and the instance (object) of the class allocates space from it. Java has the ability to collect useless units: it does not allow programmers to explicitly release objects. Java does not specify the useless Unit collection algorithms used. Various algorithms can be used according to system requirements.

5. Method Area
The method area is similar to the compiled code in a traditional language or the text segment in a Unix process. It saves the method code (Compiled java code) and symbol table. In the current Java implementation, the method code is not included in the useless Unit collection heap, but is planned to be implemented in future versions. Each class file contains the compiled code of a Java class or a Java interface. It can be said that the class file is the Execution Code file of the Java language. To ensure that the platform does not have files

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.