Java
Program execution
Before diving into Java, let’s understand how Java source files are executed.
- Use the editor or
IDE
(Integrated Development environment) writtenJava
Source file. That isSimple.java
- Programs must be compiled into bytecode files,
javac
(Java
Compiler) compiles the source file asSimple.class
File. - Class files are available on any platform/operating system
JVM
(Java
Virtual machine) JVM
Translate bytecode files into machine code that can be executed by the machine (0,1 binary)
JVM(Java Virtual Mechine)
The JVM(JAVA Virtual machine) is a virtual machine that runs JAVA bytecodes, which are generated by compiling.java files into.class files. Class files contain bytecodes that the JVM can understand.
In the real world, the JVM is a specification that provides a runtime environment that can execute Java bytecode. Different vendors provide Different implementations of this specification. Visit Different JVM Implementations to see
The most popular implementation of the JVM is Hotspot, which is owned and maintained by Oracle.
The JVM uses a number of advanced technologies to provide optimal performance for Java programs, including an advanced memory model, garbage collector, and adaptiveOptimizer
There are two different styles of JVM -client and Server. Although Server and Client VMs are similar, The Server VM has been specially tuned to maximize peak operating speed. It is used for long-running server applications that need to run as fast as possible, rather than fast startup times or smaller run-time memory footprint. Developers can select the system they want by specifying -client or -server.
The JVM is called a virtual machine because it provides a machine interface that is independent of the underlying operating system and machine hardware architecture.
This independence from hardware and operating system makes Java programs “write-once-run-anywhere.”
The JVM structures
Class Loacer
A class loader is a subsystem used to load class files. Its main functions are loading, linking, and initialization.
Loading
-
Loading ways of the class is loaded, the JVM has three kinds: the Bootstrap, the extension and application
-
When the class file is loaded, the JVM will find a dependency for any class XYZ. Class.
-
The first bootstrap class loader tries to find the class by scanning the rt.jar file in the lib folder
-
If no class is found, the Extension class loader looks for the class in the JRE \lib\ext folder
-
The application class loader will query all Jar files and classes in the system CLASSPATH environment variable.
-
If the class is found by any loader, it is loaded by the classloader, otherwise an exception is thrown :ClassNotFoundException.
Linking
- When the class load is complete, execute
Linking
(link), a bytecode validator will verify that the generated bytecode is correct, and get a validation error if the validation fails. At this point, memory is also allocated to static variables and static methods in the class.
Initialization
- This is the final stage of class loading, where all static variables are assigned their original initial values and static code blocks are executed.
JVM Memory Areas
JVM memory regions are divided into sections to store specific portions of application data.
- **
Method Area
(method area): ** stores image metadata, constant run pool, method code structure. - Heap: Stores all objects that are created while the program is running
Stack
(Stack area) :Stores local variables (local variables) and intermediate results. All of these variables are local to the thread that created them. Each thread has its ownJava Stack
All of these local variables are also called:Thread local variable
PC register
(Program counter) :The physical memory address that stores the statement currently being executed. inJava
, each thread has its own independentPC register
.Java
Native code is supported and can be used. Much of the underlying code is created byC/C++
To write. The local method stack holds instructions for local code.
JVM Execution Engine
All code assigned to the JVM is executed by the Execution Engine, which reads the bytecode and executes it one by one. It uses two built-in interpreters and a JIT compiler to convert bytecode into machine code and execute it.
With the JVM, both interpreters and compilers produce native code, differing in how they generate it, how they optimize it, and at what cost.
Interpreter(Interpreter)
By looking for a predefined JVM-instruction to machine instruction map, the JVM interpreter can translate each bytecode instruction into its corresponding local instruction. It executes bytecode directly, without any optimizations.
JIT Compiler(even if the Compiler)
-
To improve efficiency, the JIT Compiler interacts with the JVM at run time, and appropriately compiles bytecode sequences into native machine code. Typically, JIT Compiler executes a piece of code (not one statement at a time), optimizes that piece of code, and translates it into optimized machine code.
-
JIT Compiler is enabled by default and you can disable it, in which case the entire Java program will be interpreted, so it is not recommended to disable JIT Compiler, except for diagnostic or JIT compilation problems of course.
What is the JRE
The Java Runtime environment (JRE) is a software package that bundles libraries(JARS) and JVMS, as well as other components of applications written in Java. The JVM is only part of the JRE release.
To execute any Java program, you need to have a JRE installed on the machine, which is the minimum requirement.
The following components are bundled with the JRE:
-
DDL files: used by Java client VMS.
-
Code libraries, property Settings, and resource files are also included, such as rt.jar and charsets.jar
-
Java extension files, such as localedata.jar
-
Contains files for security management, including security policies and security properties
-
Jar file containing applets support classes
-
Contains TrueType font files used by the platform
The JRE can be downloaded as part of the JDK or separately, and the JRE is platform-dependent, which means that depending on the machine type (operating system and architecture), you must select the JRE package to import and install
JDK
The JDK is a superset of the JRE, and contains all the tools for developing, debugging, and monitoring applications for the JRE. When you want to develop Java applications, you need to install the JDK.
Here are some important components that come with the JDK:
-
Apt annotation processing tool
-
Javadoc document generator that automatically generates documentation from source code
-
Jar archive, which packages related class libraries into a JAR file. It also helps manage JAR files
-
JConsole Java monitoring and management platform
-
Jhat Java heap analysis tool
-
Jstack prints stack information for Java threads
-
Keytool Policy creation and management tool
-
Jarsigner Java signature and validation tool
Like the JRE, the JDK is platform-dependent, so download the appropriate JDK package for your machine
Differences between JDK, JRE, and JVM
Based on the above brief introduction, we can draw the following differences:
JRE = JVM + libraries to run Java Application
JDK = JRE + tools to develop Java Application
Copy the code
Simply put, if you’re a Java application developer writing code, you’ll need to install the JDK, and if you just want to run applications built in Java, you’ll just need to install the JRE.
JDK, JRE, and JVM
-
There are several types of class loading in Java
Three, respectively is: the Bootstrap, extendsion, application
-
How does a class loader work
Class loaders scan their predefined JAR files and class locations. They scan all the class files in the path and look for the required classes. Load, link, and initialize the class file if it is found.
-
The difference between JRE and JVM
The JVM is the specification of the runtime environment that executes Java applications. The Hotspot JVM is an implementation of this specification. It loads the class file, uses interpreter and JIT Compiler to convert bytecode to machine code, and executes it.
-
Interpreter and JIT Compiler differences
Interpreter interprets the bytecode line by line and executes it in order. This leads to poor performance. JIT Compiler analyzes the code in blocks and adds optimizations in the process, then prepares (compiles) it for more optimized machine code.