Understand the difference and connection between jvm, jre, and jdk in Java

Sometimes the more basic the problem is, the easier it is to be ignored by us. For example, the problem in the title feels very simple, but if you really explain it, it may not be very clear. In fact, this problem is explained in the Java document on Oracle's official website. It is very clear, look at the picture below the official website:






first explain the meaning of the following terms:

(1) JVM (Java Virtual Machine): The Java Virtual Machine

is mainly responsible for interpreting the class file, that is, the bytecode encoding, as 010101 that the machine can recognize Similar coding, and then execute the program. Because the JVM has corresponding implementations on mainstream platforms, we often say that the JVM is cross-platform. It only needs to be written once and can be run anywhere. In fact, the JVM shields the details of the underlying operating system.

(2) JRE (Java Runtime Environment): The Java runtime environment


jre includes jvm, in addition to some java built-in Libraries, such as collection classes, concurrency classes, mathematical functions, etc.


(3) JDK (Java Development Kit): Java development The toolkit

jdk includes jre, and additionally provides functions for compiling and debugging java source code, etc., so JDK is a component that every Java developer must install.




As can be seen from the above figure:

jdk includes jre, and jre includes jvm. If you only need to run java programs, you only need to install one jre. If you want to develop and debug, you must install JDK.




Next, think about a question, how is the java source file we wrote executed?

Look at the following picture:






It mainly goes through the following four processes:

(1) First, the developer writes the program, that is, our source code file

(2) The JDK compiles it into a class file. Note that this file is bytecode and cannot be read by the human eye.

(3) The Class Loader in the JRE is responsible for loading the bytecode file and then verifying it. , prepare, parse. Note that loading this block can read the class file from the network or from the local disk.


(4) Finally, the JVM is responsible for interpreting the bytecode into the 01010 code recognized by the machine, and then initializes the program and is responsible for execution.




In the whole process, there are compilation and interpretation operations in Java, so it can be understood that Java is a compilation and interpretation process. language. In the whole process above, there is a step, which is more critical, which is the class loader.


Regarding the working mode of the class loader in Java, we can see the following picture:





The class loader in Java adopts the parent delegation mode. As can be seen from the above figure, when loading the file name of a class, it will first Start searching from the first-level boot loader. If not, return to the second-level extended class loader to find the class file. If there is no more, look for it from our application loader, that is, the system-dependent package. If If you find it, go to the next steps. If it is still not found, a ClassNotFoundException exception will be thrown. Another common exception in daily development is ClassNotDefFoundException. This exception is when a.class is loaded, and b.class is referenced. If b. If the class is not found, this exception will occur. Understanding these details will be of great help to our development and debugging.



Summary:


This article mainly introduces several important concepts in Java, namely JVM, JRE, JDK and their workflow. In addition, it also focuses on the working principle of class loaders and understands their working modes. There are To help us better use the Java language, of course, the specific runtime memory data structure and garbage collection mechanism of the JVM are not discussed in detail in this article, which can be summarized later when there is time.



Reference link:

https://docs.oracle.com/javase/8/docs/index.html

http://www.careerbless.com/java/basics/JavaArchitecture.php

If you have any questions, you can scan the code and follow the WeChat public account: I am attacking City division (woshigcs), leave a message in the background for consultation. Technical debts cannot be owed, and health debts cannot be owed. On the road of seeking the Tao, walk with you.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326428473&siteId=291194637