Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of Java virtual machine

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the use of the Java virtual machine, the article is very detailed, has a certain reference value, interested friends must read it!

What is the Java virtual machine

The Java virtual machine is an imaginary machine, which is realized by software simulation on the actual computer. The Java virtual machine has its own imaginary hardware, such as processors, stacks, registers, etc., as well as corresponding instruction systems.

1. Why use the Java virtual machine?

A very important feature of Java language is its platform independence. The use of Java virtual machine is the key to realize this feature. If a general high-level language is to run on different platforms, it needs to at least be compiled into different object code. After the introduction of Java language virtual machine, Java language does not need to be recompiled when running on different platforms.

The Java virtual machine shields the information related to the specific platform, so that the Java language compiler only needs to generate the object code (bytecode) running on the Java virtual machine and can run on a variety of platforms without modification. When executing the bytecode, the Java virtual machine interprets the bytecode as the execution of machine instructions on the specific platform.

two。 Who needs to know about the Java virtual machine

The Java virtual machine is the basis of the underlying implementation of the Java language, and anyone interested in the Java language should have a general understanding of the Java virtual machine. This helps to understand some of the nature of the Java language, as well as to use the Java language.

For the software personnel who want to implement the Java virtual machine on a specific platform, the compiler writers of the Java language and those who want to implement the Java virtual machine with hardware chips, they must have a deep understanding of the specification of the Java virtual machine.

In addition, if you want to extend the Java language, or compile other languages into Java bytecode, you also need to learn more about the Java virtual machine.

Data types supported by 3.Java virtual Machin

The basic data types that the Java virtual machine supports the Java language are as follows:

Byte://1 byte signed integer complement short://2 byte signed integer complement int://4 byte signed integer complement 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 checking is done at compile time. The data of the raw data types listed above does not need to be marked with hardware when executed by Java. The bytecode (instruction) that manipulates these raw data types already indicates the data types of operands, such as iadd, ladd, fadd, and dadd instructions, which add two numbers, especially int, long, float, and double.

The virtual machine does not set separate instructions for the boolean (Boolean) type. Boolean data is processed by integer instructions, including integer returns. Boolean arrays are processed with byte arrays.

The virtual machine uses floating-point numbers in IEEE754 format. Older computers that do not support the IEEE format may be very slow when running the Java numerical calculation program.

Other data types supported by virtual machines include:

Object// references returnAddress//4 bytes to the 4 bytes of a Javaobject (object) for use in jsr/ret/jsr-w/ret-w instructions

Note: Java arrays are treated as object.

The specification of virtual machines does not have any special requirements for the internal structure of object. In Sun's implementation, a reference to object is a handle that contains a pair of pointers: one pointer to the object's method table and the other to the object's data.

Programs represented by the bytecode of the Java virtual machine should comply with the type rules. The implementation of the Java virtual machine should refuse to execute bytecode programs that violate the type specification.

The Java virtual machine seems to run only on machines in 32-bit address space due to bytecode definition limitations. But you can create a Java virtual machine that automatically converts bytecode into 64-bit form.

From the data types supported by the Java virtual machine, we can see that Java strictly regulates the internal format of the data type, which makes the interpretation of the data in the implementation of various Java virtual machines the same, thus ensuring the platform independence and portability of Java.

II. Java virtual machine architecture

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

Java instruction set

The Java virtual machine supports approximately 248 bytecodes. Each bytecode performs a basic CPU operation, such as adding an integer to a register, subroutine transfer, and so on. The Java instruction set is equivalent to the assembly language of Java programs.

The instructions in the Java instruction set contain a single-byte operator that specifies the operation to be performed, as well as 0 or more operands that provide the parameters or data required for the operation. Many instructions have no operands and consist only of a single-byte operator. The inner loop of the virtual machine is executed as follows:

Do {take an operator byte; perform an action based on the value of the operator;} while (program not finished)

Because of the simplicity of the instruction system, the process of execution by the virtual machine is very simple, which is helpful to improve the efficiency of execution.

The number and size of operands in an instruction are determined by the operator. If the Operand is larger than a byte, then the order in which it is stored is the high-order byte. For example, a 16-bit parameter takes up two bytes when it is stored, and its value is: first byte * 256 + second byte

Bytecode instruction streams are generally only byte aligned. The exceptions are the instructions tableswitch and lookup, which require mandatory 4-byte boundary alignment within these instructions.

Register

The registers of the Java virtual machine are used to save the running state of the machine, similar to some special registers in the microprocessor. There are four registers for the Java virtual machine:

Pc:Java program counters. Optop: a pointer to the top of the Operand stack. Frame: a pointer to the execution environment of the current execution method. Vars: a pointer to the first variable in the local variable area of the currently executing method.

Java virtual machine

The Java virtual machine is stack, it does not define or use registers to transfer or accept parameters, its purpose is to ensure the simplicity of the instruction set and the efficiency of implementation (especially for processors with a small number of registers). All registers are 32-bit.

Stack

The stack of Java virtual machine has three areas: local variable area, running environment area and Operand area.

Local variable area

Each Java method uses a fixed set of local variables. They are addressed according to the word offset from the vars register. Local variables are 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, a local variable with index n, if it is a double-precision floating-point number, actually occupies the storage space represented by indexes n and nforth 1.) The virtual machine specification does not require that 64-bit values in local variables are 64-bit aligned. Virtual machines provide instructions to load values in local variables into the Operand stack and to write values in Operand stacks to local variables.

Operating environment area

The information contained in the runtime environment is used for dynamic linking, normal method returns, and exception propagation.

Dynamic link

The runtime environment includes pointers to the interpreter symbol table for the current class and method to support dynamic linking of method code. The class file code for the method uses symbols when referencing the method to be called and the variable to be accessed. Dynamic linking translates symbolic method calls into actual method calls, loads the necessary classes to interpret symbols that have not yet been defined, and translates variable access to offset addresses corresponding to the storage structure in which these variables are run. Dynamically link methods and variables so that changes in other classes used in the method do not affect the code of the program.

The normal method returns

If the current method ends normally, the called method gets a return value when a return instruction of the correct type is executed.

The execution environment is used to recover the caller's register under normal return and increase the caller's program counter by an appropriate value to skip the executed method invocation instruction, and then continue execution in the caller's execution environment.

Exception and error propagation

The exception is called Error (error) or Exception (exception) in Java, which is a subclass of the Throwable class. The reason for the error in the program is: ① dynamic link error, such as unable to find the required class file. ② runtime error, such as a reference to a null pointer

The program uses throw statements.

When an exception occurs, the Java virtual machine takes the following actions:

Check the catch clause table associated with the current method. Each catch clause contains its valid instruction scope, the type of exception that can be handled, and the address of the code block that handles the exception.

The catch clause that matches the exception should meet the following conditions: the instruction that caused the exception is within the scope of its instruction, and the type of exception that occurs is a subtype of the type of exception it can handle. If a matching catch clause is found, the system moves to the specified exception handling block for execution; if no exception handling block is found, the process of finding a matching catch clause is repeated until all nested catch clauses of the current method are checked.

Since the virtual machine continues to execute from the first matching catch clause, the order in the catch clause table is important. Because the Java code is structured, you can always arrange all the exception handlers of a method into a table in order, and for the values of any possible program counter, you can find the appropriate exception handling block in a linear order to handle the exception that occurs under the program counter value.

If no matching catch clause is found, the current method gets a result of an "unintercepted exception" and returns to the caller of the current method as if the exception had just occurred in its caller. If the corresponding exception handling is still not found in the caller, the system invokes a default exception handling block.

Operand stack area

Machine instructions only take operands from the Operand stack, operate on them, and return the results to the stack. The reason for choosing the stack structure is that it can also efficiently simulate the behavior of virtual machines on machines with only a small number of registers or non-general registers, such as Intel486.

The Operand stack is 32-bit. It is used to pass parameters to the method and to receive the results from the method. It is also used to support the parameters of the operation and to save the results of the operation.

For example, the iadd instruction adds two integers. The two integers added should be the two words at the top of the Operand stack. These two words are pushed into the stack by previous instructions. The two integers will pop up from the stack, add them, and press the result back into the Operand stack. Each primitive data type has special instructions to perform the necessary operations on them. Each Operand needs a storage location in the stack, except for the long and double types, which require two locations.

Operands can only be operated by operators of their type. For example, it is illegal to press two numbers of type int if they are treated as a number of type long. In Sun's virtual machine implementation, this limitation is enforced by bytecode validators. However, there are a few operations (operators dupe and swap) that are used to operate on the runtime datazone regardless of type.

Useless unit collection reactor

The Java heap is a run-time data area from which instances (objects) of the class allocate space. The Java language has the ability to collect useless units: it does not give programmers the ability to explicitly release objects. Java does not specify the specific collection algorithm of useless units, and can use a variety of algorithms according to the needs of the system.

Method area

A method zone is similar to compiled code in a traditional language or a body segment in a Unix process. It holds the method code (compiled java code) and the symbol table. In the current Java implementation, the method code is not included in the garbage collection heap, but is planned to be implemented in a future version. Each class file contains compiled code for a Java class or a Java interface. It can be said that the class file is the execution code file of the Java language. In order to ensure the platform independence of class files, the format of class files is also described in detail in the Java virtual machine specification. For details, please refer to Sun's Java virtual machine specification.

3. Java class files (.class files)

Each class file contains compiled code for a Java class or a Java interface. It can be said that the class file is the execution code file of the Java language. In order to ensure the platform independence of class files, the format of class files is also described in detail in the Java virtual machine specification. For details, please refer to Sun's Java virtual machine specification.

4. Java chip

Because Java is an interpretive language, it runs much slower than compiled languages on general-purpose processors. In order to further promote the application of Java, Sun Company has launched Java chip. These chips can be said to be the hardware implementation of Java virtual machine. Unlike the virtual machine composed of general chip and interpreter, Java chip can execute bytecode directly, that is to say, bytecode is the instruction set of Java chip. This kind of virtual machine implemented in hardware is certainly much faster than the virtual machine simulated by software.

Sun plans to launch three kinds of Java chips: picoJava,mi-croJava and ultraJava.

PicoJava is a very small Java core chip with a wafer area of only 25mm2. Sun is ready to sell to other companies so that other companies can customize their own Java chips, whose low-end products are estimated to sell for less than $25.

MicroJava is a microcontroller chip based on picoJava, whose chip area is about 50mm2, which is used for communication devices and other embedded control devices. Sun hopes that Java chip can become one of the main technologies to promote the development of Java.

These are all the contents of the article "what is the use of the Java virtual machine?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report