Why is java written once and run everywhere? (Simple understanding of the relationship between JDK, JRE, and JVM)


foreword

This article is mainly divided into two parts: understanding the principle of java cross-platform (ie: write once and run everywhere), and
the relationship between JDK, JRE, and JVM in java.


1. Java cross-platform principle

Java is an excellent programming language, it can be written once and run everywhere.

(1) Reasons why other languages ​​cannot achieve cross-platform

a. Differences at the interface level: For example: windows and linux have their own system-level API functions ( API—application program interface ), and the operating system needs to call in addition to coordinating the execution of application programs, memory allocation, and system resource management. Various services (each of which is a function), so it varies from operating system to operating system.

b. Different hardware devices: refers to different instruction sets (different device processors (CPU architecture), so the corresponding instruction sets are different)

(2) How does java do it?

a. At the interface level: Java directly includes the system-level API. For example, the commonly used System class, packaging class, date and time class, Math class, StringBuffer, StringBuilder class, etc.

b . At the level of hardware devices: java programs are compiled to generate bytecode files . (Java virtual machine) translates it into instructions that the corresponding machine can recognize, and can be compiled on different operating systems.

The diagram is as follows:
insert image description here


Two, java running process

(1) Briefly understand jvm:
jvm: java virtual machine , which is a software written in C language. The main function is to translate bytecode file instructions into machine-recognizable instructions.

The running process of java is as follows:
insert image description here
Compared with the running process of C language:
insert image description here
(2) Java compilation

The compilation of Java generates a bytecode file (.class) through the command javac (ie javac.exe, which is an executable file written in pure Java language in the JDK installation bin directory) . Then execute the Java program in the JVM.
insert image description here


3. The relationship between JDK, JRE, and JVM

The jvm (Java Virtual Machine) was introduced earlier, which is simply understood as: translating bytecode file instructions into instructions that the machine can recognize.

(1) JDK : Java Development Kit (Java Development Kit)
JDK is the core of the entire java development, which includes the JAVA operating environment (JVM+Java system class library) and JAVA tools.
(2) JRE: Java Runtime Environment (Java Runtime Environment)
JRE allows the computer system to run Java applications. It includes Java virtual machine (jvm), Java core class library and supporting files.

The relationship between the three is as follows:
insert image description here


Summarize

That's all for this article.

Guess you like

Origin blog.csdn.net/m0_53689542/article/details/123724509