For programmers, the 80/20 rule means that you spend 80 percent of your time learning the 20 percent of principles that are uncommon in everyday development.
It is said that a programmer of Ali is very interested in calligraphy, after retirement decided to do something in this field. So he spent a lot of money to buy the four treasures of the study.
One day, after dinner, a sudden excitement came into my mind. I rubbed the ink to make a paper, and lit up the good sandalwood, with the style of Wang Xizhi and the momentum of Yan Zhenqing. After a moment of calm, I splashed ink and solemnly wrote a line: hello world.
Of course, this is a programmer joke lol.
So the question is, after writing Hello World for so long, are you sure you understand what’s behind what you’re writing? (o ≖ ◡ ≖)
Given 2 minutes, this knowledge covers Java program execution process, including compilation, loading and execution, can you understand?
Now for serious time (@ ̄ ー ̄ @)
Different Hello World
public class Main {
private static String word = "Hello World!";
public static void main(String[] args) {
new Main().say();
}
private void say() { System.out.println(word); }}Copy the code
The entire code execution process can be divided into three stages:
- The code to compile
- Class loading
- Class to perform
The code to compile
The Main class file is called a bytecode file. When you open it, it’s a bunch of Mars. In this case, we can think of the compilation process as the process of producing the raw material of the JVM. The tool used is javac, a tool provided by the JDK. The general process is as follows:
- Lexical analysis is the process of turning the character flow of source code into Token set. Are described, the vernacular Chinese in our actual programming, single character is the smallest unit, and actually in the process of programming, the tag is the smallest unit, such as keywords, variables, literals, operators, etc can be a Token, seems I still have a veiled, for example (> man “), such as integer int in our programming which is composed of three characters, I, n, and T are three characters, and during the compilation process, it is a Token and cannot be split.
This process is completely shielded from us, but it is actually a modern classical compilation principle, and the lexical analysis is also used to prepare for later compilation.
- Syntax analysis, after obtaining Token set through lexical analysis, the next step is to build an abstract syntax tree, the so-called abstract syntax tree is actually a kind of used to describe the syntax tree representation of the structure of program code, including syntax tree each node represents a grammatical structure in the program code, such as package, type, modifier, operators, etc.
Main. Java already has a clear understanding of what it is writing, but the JVM is still confused, so we need to build a syntax tree. After this step, there is no further operation on the source file, and the rest of the operation is based on the abstract syntax tree
-
Fill in a symbol table. A symbol table is a table composed of a set of symbol addresses and symbol information. This table is used at various stages of compilation, such as during object code generation, when symbol names are assigned addresses.
-
Semantic analysis and semantic analysis also can saying is the semantic detection phase, the above said to the grammatical analysis will construct a syntax tree, so the syntax tree is correct and reasonable, is made by semantic analysis, semantic analysis by annotating check and the analysis of data and control flow check two steps, the last step of the generated bytecode information control.
-
Bytecode generation, the final stage of the JavAC compilation process, involves not just converting the information generated in the previous steps into bytecode and putting it on disk, but also a small amount of code addition and conversion, such as our own overloaded constructors.
Compile time ends here, so who delivers the raw material to the JVM virtual machine? This is where class loading comes in.
Class loading
Class loading simply means loading the compiled bytecode file [main.class] into the virtual machine by a classloader, so it is natural to introduce four types of loaders first
Talk about the four types of loaders
As you can see from the figure above, there are four classes of classloaders, and the fourth class is implemented by ourselves. What role do the other three CLASSES provided by the JVM play in starting the Main program?
The Bootstrap ClassLoader is used to load the %JAVA_HOME%\jre\lib\rt.jar class library into the memory of the virtual machine. Rt. jar contains the Java base class library, which is the Java doc class file, interested friends can open their own directory to see.
The Extension ClassLoader is responsible for loading all class libraries in the JAVA_HOME/jre/lib/ext directory, mainly for loading Extension packages.
The system ClassLoader, also known as the Application ClassLoader, is responsible for loading the class libraries specified on the user CLASSPATH (i.e., our configured CLASSPATH), which is the default class load in the Application.
After reading the above three simple description of class loaders, is there a kind of finally understand the role of the JDK environment we configured, yes, to enable class loaders to recognize and then load various class libraries?
So the question is? Which class loader loaded our Hello World program? Yes, the default ClassLoader in your Application.
Now that we know the classloader, we need to know how the classloader loads.
Talk about the class loading process
I found a picture on the Internet. Simple and clear. Simple and straightforward, but extremely important, since it is an interview hotspot (✿ retract item)
loading
The Application ClassLoader loads the compiled main. class file into memory.
The JVM divides memory into several modules, so which module is loaded into? Hot interview questions, answers at the end!
link
There is a trilogy of links that are responsible for incorporating the binaries of main.class into the JRE.
About the trilogy, it’s easy to understand;
Firstly, in the verification stage, the class loader loads the binary byte stream into the virtual machine, which definitely needs to be verified to avoid endangering the virtual machine’s own security, which is also the value of the existence of the verification stage.
Next comes the preparation phase, where memory is formally allocated for class variables and default values are set for class variables, such as in the HelloWorld program above
private static String word = “Hello World!” ;
Notice that I’m describing first the class variable, which is what static describes, and then the default value, which is null for word above, or zero if it’s a number.
Finally, there’s the parsing phase, which is basically the process of replacing the symbolic references in the constant pool with direct references. The parsing phase is actually a little bit harder to understand, or at least a little bit harder to understand than the previous two phases, so I’ll try to be as straightforward as POSSIBLE here;
So-called symbolic reference refers to contain the class information, the method name, such as method parameters of string, and when the first run, the JVM will according to this line of string to the corresponding method to retrieval entrance, and in order to don’t have to do the same to retrieve next time, when the first run will be symbols refer to replace direct reference, In this way, a certain amount of consumption can be saved behind; In this case, the direct reference refers to the offset, which allows the VIRTUAL machine to directly find the method entry without any retrieval.
The default value of Word is null, which is the default value assigned by the system. During initialization, we initialize the class variables and other resources manually. For example, word is initialized to “Hello World!” .
Class to perform
Now that the main.class is loaded into the Java virtual machine memory, it’s time to execute. So who will perform this process? As shown in figure
- Class loading subsystem
- Execution engine subsystem
- Garbage collection subsystem
Obviously, clearly, the class loading subsystem in the diagram has been discussed above, the execution engine subsystem is responsible for executing this part, so what is the process like?
It’s pretty simple. The execution engine converts bytecode to machine code. Bytecode is the language recognized by the JVM, but machine code is the language eventually recognized by the operating system.
Then the operating system can actually call the JIT, many people who learn or do Java have heard of JIT, but do not know exactly what it is, yes, it is you.
The bytecode to machine code translation is done using the JIT (Just In Time) compiler (which compiles the entire hot code) and the Java bytecode interpreter (which interprets bytecode line by line). Here is the JIT compilation workflow:
JVM bytecode -> machine independent optimization -> intermediate code -> Machine related optimization -> intermediate code -> Register allocator -> intermediate code -> Target machine code generator -> target machine code
Finally, the execution engine finds the entry method main() and executes its bytecode instructions.
Finally, about the HelloWorld execution process, basically elaborated, about the execution of the program, JVM memory allocation problem, is a relatively large module, for more details, please pay attention to the public number, we will talk next time!!
After the loading phase is complete, the virtual machine stores the binary stream of main. class in the method area in the format required by the virtual machine, and then instantiates an object of class Java.lang. class in memory to act as an external interface for the program to access these types of data in the method area. Instantiated java.lang.Class objects are also stored in the method area.
Public number main: server programming related technical explanation, specific can see the historical articles.
Public account sideline: all kinds of chat water, including technology, employment, life experience, university life, push and so on.
Welcome to watch and chew the fat together