Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation principle of Java?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the implementation principle of Java". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the implementation principle of Java".

First, write the java source program

Java source file: refers to the file that stores the java source code.

Let's take a look at the following code:

/ / MyTest is modified by public, so the file that stores the java source code is called MyTest

Public class MyTest {

Public static void main (String [] args) {

System.out.println ("Test Java execute process.")

}

}

/ / because MyTest is modified by public, Class A cannot be modified with public

Class A {}

/ / because MyTest is modified by public, Class B cannot be modified with public

Class B {}

1. The name of the java source file is the name of the public class in the source file

2. A java source file can contain multiple classes, but only one class is allowed to be public

II. Compile the java source code

When the coding of the java source program is finished, the compiler is required to compile.

After installing jdk, we open the jdk directory and there are two .exe files, namely javac.exe (compile source code, xxx.java file) and java.exe (execute bytecode, xxx.class file).

As shown in the following figure:

1. Switch to the MyTest.java folder

2. Javac.exe compiles MyTest.java

After compilation, it is found that the e:\ Blogs directory has more files with the suffix of class: A.classPowerB.class and MyTest.class.

Tip: when javac.exe compiles java source code, the java source code has several classes and is compiled into a corresponding bytecode file (.class file)

Where the file name of the bytecode file is the class name of each class. It is important to note that even if the class is not defined in the source file, it is referenced by the source file, and after compilation, the corresponding bytecode file is programmed.

For example, class A refers to class C, but class C is not defined in the source file of class A. after compilation, class C is also compiled into the corresponding bytecode file C.class

Tips: follow Wechat official account: Java backend, and get technical blog posts and tweets every day.

Third, execute java source files

Execute the java source file, which can be executed with java.exe

Up to now, the java source program basically executes the results and prints the desired results correctly, so, as shown in the steps above, we can summarize as follows:

As summarized above, execution in JVM has been abstracted. Next, we will analyze how bytecode files (.class files) are executed step by step in the virtual machine.

4. How to execute bytecode files in JVM

1. Load bytecode files

When the .java source code is compiled into a .class bytecode file by the javac.exe compiler, the rest of the work is left to JVM.

JVM first loads the class file and related Java API into JVM through the class loader (ClassLoader) for subsequent processing by JVM.

In this stage, the following basic concepts and knowledge are involved.

1) relationship between JDK,JRE and JVM

JDK (Java Development Kit), Java development kit, mainly for development. Before JDK7, JDK included JRE

JRE (Java Runtime Environment), the core environment in which Java programs run, including JVM and some core libraries

JVM (Java Virtual Machine), VM is a specification for computing devices. It is a fictional computer, which is realized by simulating various computer functions on a real computer. It is the core module of JRE.

2) JVM

JVM is a specification for computing devices. It is a fictional computer, which is realized by simulating various computer functions on a real computer.

The main task of the Java virtual machine is to load the class file and execute the bytecode in it. Different Java virtual machines may have different implementation of the execution engine.

There are roughly several types of engines:

One-time interpretation bytecode engine

Just-in-time compilation engine

Adaptive optimizer

With regard to the implementation of virtual machines, software, hardware and the combination of software and hardware are adopted, which depends on the specific manufacturer.

3) what is ClassLoader

The main task of the virtual machine is to load the class file and execute the bytecode in it, while the class file is completed by the virtual machine's class loader (ClassLoader). There may be multiple class loaders in a Java virtual machine.

Any java application may use two types of loaders, the startup class loader (bootstrap) and the user-defined class loader.

Boot class loader is the only part of Java virtual machine, which can be divided into original class loader, system class loader or default class loader. Its main function is to load the corresponding classes, such as Java API classes, from the disk of the operating system.

User-defined loading classes, that is, loading classes in a user-defined way.

2. Store the bytecode file in the JVM memory area

When the JAVA virtual machine runs a program, it needs memory to store a lot of things.

Such as bytecode, objects created by the program, parameters passed to methods, return values, local variables, and intermediate results of operations, which are organized into "run-time data areas".

The runtime datazone varies from vendor to vendor in the Java virtual machine. Some runtime data areas are shared by threads, while others can only be shared by a specific thread.

The runtime data area can be roughly divided into several areas: method area, stack area, stack area, PC register area and local method stack area.

In this stage, the following basic concepts and knowledge are involved.

1) method area

The method area is used to store information about parsing the loaded class file.

When a class file is loaded virtually, it parses the type information from the binary data contained in the class file, and then stores the related information in the method area.

2) heap

Heaps are used to store related reference types, such as new objects. When the program is running, the virtual machine puts all the objects created by the program at run time on the heap.

3) PC register

PC registers are mainly used to store threads. When a new thread is created, the thread gets its own PC register (program counter) and a java stack.

The Java virtual machine does not have registers, and its instruction set uses the Java stack to store intermediate data.

4) Stack area

The stack area is mainly used to store value types, such as basic data types. It is important to note that String is a reference type and exists in the heap.

The Java stack consists of many stack frames. One stack frame contains the state of a Java method call. When a thread calls a method, the virtual machine pushes a new stack frame into the thread's Java stack, and when the method returns, the stack frame pops up from the Java stack.

3. The execution engine interacts with the runtime data area

The runtime data area provides the execution environment and related data for the execution engine. By interacting with the runtime data area, the execution engine obtains the relevant information needed during execution and stores the intermediate results of execution.

4. Interface between execution engine and local method

When a local method is to be executed, the execution engine calls the local method interface to get the relevant OS local method.

It is important to note that the local method is strongly coupled to the operating system.

5. JVM is executed on a specific operating system

JVM obtains the local method by calling the local interface, which can be executed on the specific platform. Such as execution on Linux systems, execution on Window systems, and execution on Unix systems.

Thank you for your reading, the above is the content of "what is the implementation principle of Java". After the study of this article, I believe you have a deeper understanding of what the implementation principle of Java is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report