Java Virtual Machine-Linux general technology-Linux programming and kernel information. The following is a detailed description. 1. What is a Java Virtual Machine?
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.
1. Why should I use a 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.
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.
2. Who needs to know about 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.
3. Data Types supported by Java Virtual Machine
Java Virtual Machine supports the following basic data types:
Byte: // The complement code of a 1-byte signed integer short: // The complement code of a 2-byte signed integer int: // The complement code of a 4-byte signed integer long: // 8-byte signed integer complement float: // 4-byte IEEE754 single precision Floating Point double: // 8 byte IEEE754 double Precision Floating Point 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, its operand types include 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 returnAddress for a Javaobject (object) // 4-byte for jsr/ret/jsr-w/ret-w instructions
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 portable.
Ii. 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.
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 {Get An operator byte; execute an action based on the operator value;} while (the program is not finished)
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. The value is the first byte x 256 + the second byte.
The bytecode instruction stream is generally only byte aligned. The command tableswitch and lookup are exceptions. The two commands require forced 4-byte boundary alignment.
Register
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.
Java Virtual Machine
The Java Virtual Machine is stack-based. It does not define or use registers to pass or accept parameters. Its purpose is to ensure the simplicity of the Instruction Set and the actual efficiency (especially for processors with a small number of registers ). All registers are 32-bit.
Stack
The stack of a Java Virtual Machine has three areas: local variable zone, Runtime Environment zone, and operand zone.
Local Variable Area
Each Java method 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 64-bit values in local variables to be 64-bit aligned. virtual machines provide instructions to load values in local variables to the operand stack, it also provides instructions to write values in the operand stack into local variables.
Runtime Environment zone
The information contained in the running 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
Exception is called Error or Exception in Java and is a subclass of the Throwable class. The cause of an Error 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.
Since 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 is found, the current method returns a result of "no exceptions intercepted" and 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 system calls a default Exception Handling block.
Operand Stack
Machine commands only take the operands from the operand 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.
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.
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 platform independence of class files, the Java Virtual Machine specification also describes the format of class files in detail. For details, see Sun's Java Virtual Machine specifications.
Iii. Java class files (. class files)
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 platform independence of class files, the Java Virtual Machine specification also describes the format of class files in detail. For details, see Sun's Java Virtual Machine specifications.
Iv. Java chips
Java is an interpreted language, which is much slower than compiled languages in general-purpose processors. Sun has launched a Java chip to further promote Java applications. These chips are hardware implementations of Java virtual machines. Unlike a virtual machine consisting of a general-purpose chip and interpreter, a Java chip can directly execute bytecode, that is, the bytecode is the instruction set of the Java chip. This hardware-based Virtual Machine is certainly much faster than a Simulated Virtual Machine.
Sun plans to launch three types of Java chips: picoJava, mi-croJava, and ultraJava.
PicoJava is a small Java kernel chip with a silicon wafer area of only 25mm2 square meters. Sun is planning to transfer the chips to other companies so that other companies can customize their own Java chips. The prices of its low-end products are estimated to be less than $25.
MicroJava is a picoJava-Based Microcontroller chip with an area of approximately 50mm2 square meters. It is used for communication devices and other embedded control devices. Sun hopes that the Java chip will become one of the main technologies to promote Java development.
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.