You do not know the principles behind HelloWorld

[Today] the best for programmers, so-called twenty-eight law refers spend eighty percent of the time to learn the principles that twenty percent of the daily R & D is not common.

Ali reportedly very interested in calligraphy on a programmer, decided to retire to accomplish something in this regard. So spend big money buying the finest four treasures.

One day, after a meal emergent Masaoki, some Surusumi proposed paper, and lit a sandalwood on the good, quite Wang Xizhi style, but also with Yen Chen momentum, looking hard moment, ink brush, solemnly wrote his words: hello world.

1.png

Of course, this is a unique piece of programmers ha ha ha.

So the question is, writing for so long Hello World, we determine their understanding behind what you write is what principle do? (O≖◡≖)

[Given two minutes, the knowledge related to the Java program execution flow, including compilation, loading and execution, if you can clarify it? ]

Next, enter the serious period (@ ¯ ー ¯ @)

Distinctive 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);
    }
}

复制代码

Execution of the entire code can be divided into three stages:

  • Code compilation
  • Class loader
  • Class execution

Code compilation

The role is to compile the code we write Main.java files into Main.class file, .class here is also known as byte code file, open a pile of Martian [anyway] is not read, here we can compile the JVM process as raw material production process, using tools javac jdk tool is provided. Process substantially as follows:

  • Lexical analysis , character is about to become the source of the transfer process Token set. Under the vernacular description is at our actual programming, the smallest unit is a single character, but actually in the programming process, is the smallest unit mark, such as keywords, variable names, literals, operators, and so can become Token, seemingly still somewhat blinded, for example (> ﹏ <), such as in our int integer programming it is three characters, that is, i, n, t three characters, and in the process it is a compilation the Token, not split.

This process is for us, in fact, is fully shielded, but in fact it is a modern classic routines compiler theory, but also to give back lexical analysis compiled in preparation]

  • Parsing , get the Token set by lexical analysis, the next step is to construct an abstract syntax tree, called an abstract syntax tree is actually a tree structure describing the program code syntax used to represent a way in which each node of the syntax tree all represent a grammatical structure in the program code, such as bags, type, modifiers, operators and the like.

In our eyes, Main.java already clearly understand what is written in the end, but for the JVM is forced to look ignorant, so it needs to be built into a syntax tree, no longer after this step for source files operation, and subsequent operations are built on the abstract syntax tree

  • Filling the symbol table , the symbol table is a set of tables and symbolic address information composed of symbols, this table will be used at different stages of compilation, the code generation phase as the target, the symbolic name will address allocation, the symbol table is address assignment basis.

  • The semantic analysis , semantic analysis phase can also be said semantic detection phase, the top when it comes to parsing builds a syntax tree, then the tree syntax tree whether it is right and proper, it is done by the semantic analysis, and semantic analysis will be marked by inspection data and control flow analysis and a two-step inspection starts, the last step in the generated check information byte code.

  • Byte code generation , which is the final stage of the compilation process javac byte code generation phase is not just a simple step of converting each of the previously generated information into byte code and then into disks also have a small number and add code conversion work, such as our own overloaded constructor.

Compile to end here, then who is going to transport these materials to the JVM virtual machine do? This time we should look at the process of loading the class.

Class loader

Class loader is simply by the class loader bytecode files compiled] [Main.class loaded into the virtual machine, so naturally, we must first introduce four types loader

Talk about the four types of loader

image description

As can be seen from the figure above, the class loader can be divided into four categories, while the fourth is by our own realization, then the other three provided by the JVM class loading process we started in the Main program played what role in it?

Let me talk about the boot class loader Bootstrap ClassLoader, the role of the boot class loader is mainly loaded% JAVA_HOME% \ jre \ lib \ rt.jar library, load it into memory in a virtual machine, class libraries in the end there are so rt.jar What role do? Under rt.jar contains the Java foundation class library, which is Java doc inside to see all the classes of class files, and interested friends can open your own directory look.

Followed by the extension class loader Extension ClassLoader, the role of the main extension class loader is responsible for loading the JAVA_HOME \ jre \ All libraries in the lib \ ext directory, the major expansion pack is loaded.

Furthermore is the system class loader Application ClassLoader, also known as application class loader is responsible for loading the user class path (that is, we configured CLASSPATH) on the specified library, the application is the default class loader.

Brief description of the process of reading the above three class loader is not the kind of role we finally know the final configuration jdk environment it is, is to let the class loader loads the library after various recognized.

So the question is? Which class loader to load our Hello World program it? Yes, that is the default application class loader Application ClassLoader.

After know the class loader, then the next always understand how to load the next class loader, right?

Talk about class loading process

image description

Internet to find pictures, plain and simple. Although it is simple, but really extremely important, because it is the interview hot (✿◡‿◡)

load

In fact, when it comes to system class loader above the Application ClassLoader will load Main.class compiled files into memory.

[Thinking] dished out a problem, so-called loaded into memory, we all know that the JVM memory is divided into several modules, then what is loaded into which module? Hot face questions, see the end the answer !

link

Link contains the trilogy, is responsible for the overall effect of the merger Main.class binary data to the JRE.

About trilogy, in fact, well understood;

The first is the validation phase , the class loader binary byte stream loaded into the virtual machine, certainly need to be validated, to avoid harm to their own virtual machine security, and this is the value of existing verification stage;

Next is the preparation phase , preparation phase is formally allocate memory for class variables and class variables set the default value of the place, such as the above HelloWorld program

private static String word = "Hello World!";

Note that I'm describing is a first class variable, namely the static variables described, followed by the default value, which is the default value null above word, and if the figure was zero.

Finally, the parsing stage , the role of the parsing stage is mainly symbolic references to the constant pool is replaced with a direct reference to the process, in fact, a bit difficult to understand the resolution phase, at least than the above two stages to be difficult to understand, straightforward point I try here ;

A so-called symbol means that a class contains information, character string information of the method name, parameters, and when the first run, the JVM will be to retrieve the corresponding entry in accordance with the method of this line of the string, and for the next retrieval times do not do the same, they will be replaced by a direct reference to symbolic reference, so the following will save some time consumed in the first run; direct reference here actually refers to the offset, the virtual machine can the method of offset found by direct entry, no longer need to do retrieved.

Initialization finally came to the initialization phase, the above we have when it comes to word default value is null, the default value of the system is assigned, and in the initialization phase, it is based on our human and other resources to initialize class variables, such as the above word were I initialize became "Hello World!".

Class execution

Mentioned above Main.class be loaded into the Java virtual machine memory, then the next process is performed by the. So who is going to perform this process it? Figure

image description
In fact, a Java virtual machine at run time can be divided into three subsystems:

  • Class loader subsystem
  • Execution engine subsystem
  • Garbage collection subsystem

It is clear, very clear, the figure of the class loader subsystem has been talked about above, the execution engine subsystem is responsible for implementing this part of the process is kind of how it?

Actually very simple, the execution engine will convert bytecode to machine code [what? I still have the conversion. Please <(ˉ ^ ˉ)>, JVM byte code is identified language code is the final byte is the operating system recognized language]

The operating system can really call a lot to learn Java or do people have heard of JIT, but did not know specifically why, exactly that is you.

Here finally explain the byte code into machine code using the translation work is JIT (Just In Time) time compiler (Compiler for the whole hot code) and Java bytecode interpreter (explained byte line by line code) to complete. Here JIT compiler to the next workflow:

JVM byte code -> machine independent optimization -> intermediate code -> machine-dependent optimization -> intermediate code -> register allocator -> intermediate code -> target machine code generator -> target machine code

Finally, the execution engine to find main () method of this entry, and wherein execution of bytecode instructions.

Finally, with regard to the implementation process HelloWorld, finished basically explained, during the execution of the program on, JVM memory allocation problem, is a relatively large module. For details, please pay attention to public numbers, the next time we talk to you! ! !

[Consideration] doubts the completion of the loading stage, the virtual machine Main.class binary byte stream in accordance with the storage format in the virtual machine that method area, then the memory of an instantiated object of the class java.lang.Class as the external interface access method program area of ​​these types of data, after the object class java.lang.Class instantiation is stored in the process area.


Public No. Main: server-side programming technology related commentary, concrete can see the history of the article.

No public sideline: Chit chatting all kinds, including technology, employment, life experiences, college life, interpolation and so on.

Welcome attention, together chitchatting

8.png

Guess you like

Origin juejin.im/post/5d194e745188256e98090e93