How the Java code works

Question 1: The difference between Java and C++

1. Java needs runtime environment, including Java virtual machine and Java core class library.

2. C++ does not need additional runtime, usually the compiled code can be read directly by the machine, namely machine code

Question 1: Why does Java run on a virtual machine?

1. Compile once, run everywhere.

A concept is involved here: bytecode.

Java bytecode refers to the design of a virtual machine oriented to Java features, and a compiler converts the Java program into a sequence of instructions that the virtual machine can recognize.

Why is it called bytecode? The reason is that the opcode (opcode) of the Java bytecode instruction is fixed to one byte.

2. Hosting environment.

The virtual machine can take the place of us to deal with the lengthy and error-prone parts of the code, such as the garbage collection problem that often occurs in C++, and programmers often forget to collect garbage.

In addition, the virtual machine also provides us with dynamic detection such as array out of bounds and dynamic type, which greatly reduces the difficulty of Java development.

Question 2: How does the Java virtual machine run Java bytecode?

Taking the Hotspot virtual machine as an example, the Java virtual machine is subdivided into method area, heap, Java method stack, local method stack for local methods, and PC registers.
Insert picture description here
From the perspective of the virtual machine,

1. To execute the Java code, you first need to compile it into a class file and load it into the Java virtual machine, and the loaded Java class will be saved in the method area. In actual operation, the virtual machine executes the code in the method area.

2. The heap and stack in the above figure are used to store runtime data. Among them, the stack is divided into a Java method stack for Java methods and a native method stack for local methods (native methods written in C++), and a PC register that stores the execution position of each thread

3. During operation, whenever a method is called, the virtual machine generates a stack frame in the Java method stack of the current thread to store local variables and bytecode operands. When the method is executed, the stack frame is also popped.

From a hardware perspective,
Insert picture description here
there are two ways to compile Java code in HotSpot, one is interpretation and execution, and the other is compilation and execution.

Interpretation and execution: translate the bytecode into machine code one by one and execute (read while translating)

Compile and execute: compile all bytecodes contained in a method into machine code and execute. (After translation, read again)

The former has the advantage: no need to wait for the compilation to be complete

The advantage of the latter is that when there are more hot codes, the execution efficiency of reading is faster than the former.

HotSpot uses multiple just-in-time compilers to work together: C1, C2, and Graal, which is officially used by Java 10.

Guess you like

Origin blog.csdn.net/weixin_49794051/article/details/112358616