The article directories

  • JDK, JRE, JVM
  • The HotSpot virtual machine
  • How is Java cross-platform?
  • JVM architecture
  • Stack first in last out principle
  • The underlying running process

JDK, JRE, JVM



Files in the JDK

Bin: the main compiler javac.exe includes a header file used for interaction between Java and the JVMCopy the code

Java Runtime Environment (JRE) is required for running Java programs. Java programs cannot run without jre. Without Java programs, the JRE is useless.

The HotSpot virtual machine

There are two types of VMS: Client VM and Server VM

Client VM(-client), which is optimized to reduce startup time in the Client environment; Server VM(-server), designed to maximize program execution speed in a Server environment.Copy the code

Comparison: The Server VM starts slowly and runs faster than the Client VM.

The location of the configuration file is as follows:

CFG if the vm runs a 64-bit OS {JRE_HOME}/lib/amd64/jvm. CFG if the VM runs a 32-bit OS {JRE_HOME}/lib/i386/jvm.cfgCopy the code

-server KNOWN (sercer) -server KNOWN (sercer) -server KNOWN (sercer) -server KNOWN (sercer) -server KNOWN (sercer)

You can also search for Java-version on the command console to view the VM type

How is Java cross-platform?

“Compile once, run anywhere”

Different operating systems, the underlying instructions are different, the underlying instructions can only identify the machine code (01001011)

The Java virtual machine hides the details of the underlying hardware instruction level from the software level



Java can be cross-platform because the Java VIRTUAL machine acts as a bridge. It acts as a buffer between the runtime Java program and its underlying hardware and operating system.



Q: Can C/C++ be cross-platform?

C/C++ is cross-screen at compile time, Java is cross-platform at run time.

JVM architecture

The compiled bytecode file is first loaded into memory by the ClassLoader.

The Runtime Data Area loads the bytecode into memory. The bytecode file is just a set of instructions for the JVM and cannot be delivered directly to the underlying operating system for execution.

Therefore, a specific command parser Execution Engine is required to translate bytecode into the underlying system instructions for Execution by the CPU.

In this process, it is necessary to call the Native Interface of other languages to realize the function of the whole program.

Stack first in last out principle

Heap — holds objects, shared by threads;

Stack – execution program, thread private;

One stack frame per method

The underlying running process

HelloWorld.java

package example;

/ * * *@Author:liujiamei
 * @Date: 2020/5/28 he *@Version1.0 * /
public class HelloWorld {

    public static void main(String[] argv) {

        HelloWorld hw = new HelloWorld();
        int result = hw.add();
        System.out.println(result);

    }

    /** * calculate method, local variable * a method is a stack frame: data isolation by method *@return* /
    public int add(a) {
        int a, b, c;// Store it in the local variable table of the add method
        a = 1;
        b = 2;
        c = a + b;
        returnc; }}Copy the code

Helloworld.class bytecode file



Javap code disassembly

javap
javap -c HelloWorld.class
>javap -c HelloWorld.class>hw.txt
Copy the code

hw.txt

Compiled from "HelloWorld.java" public class example.HelloWorld { public example.HelloWorld(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: new #2 // class example/HelloWorld 3: dup 4: invokespecial #3 // Method "<init>":()V 7: astore_1 8: aload_1 9: invokevirtual #4 // Method add:()I 12: istore_2 13: getstatic #5 // Field java/lang/System.out:Ljava/io/PrintStream; 16: iload_2 17: invokevirtual #6 // Method java/io/PrintStream.println:(I)V 20: return public int add(); Code: // Thread's program counter: line number pointing to the current bytecode instruction 0: iconst_1 // Push int constant 1 on the stack 1: istore_1 // store int value in local variable 1, a=1 2: iconst_2 3: Istore_2 4: iload_1 // Add int from local variable 1 to stack 5: iload_2 6: iadd Istore_3 //3 is pushed into the operand stack only 3 8: iload_3 9: ireturn // Returns int data from the method and returns it to the main method through the exit}Copy the code

JVM instruction set codeBlog.csdn.net/zhangpan199…