头图

Interviewer : Let’s start with the basics today, How do you understand that Java is a "cross-platform" language, that is, "compile once and run everywhere"?

Candidate : It's easy to understand, because we have JVM.

candidate : Java source code will be compiled into a class file, which runs on the JVM.

Candidate : When we daily develop and install JDK, we can find that JDK is divided into "different operating systems". JDK contains JVM, so Java relies on JVM to achieve "cross-platform"

Candidate : JVM is operating system-oriented, it is responsible for interpreting Class bytecode into instructions recognized by the system and executing it, and it is also responsible for the management of memory when the program is running.

Interviewer : you like to talk about the process from source file (.java) to code execution?

Candidate : Well, no problem

Candidate : In brief, I think there are 4 steps: compile -> load -> explain -> execute

Candidate : Compile: Compile the source code file into a class file that can be interpreted by the JVM.

Candidate : The compilation process will do "syntax analysis", "semantic analysis", "annotation processing" and so on to the source code program, and finally generate the bytecode file.

Candidate : For example, the erasure of generics and the Lombok we often use are done during the compilation phase.

candidate : Load: Load the compiled class file into the JVM.

Candidate : In the loading phase, several steps can be refined: loading -> connection -> initialization

candidate : Let me elaborate on these steps again.

Candidate : [Loading timing] In order to save memory overhead, not all classes are loaded into the JVM at one time, but only when it is "needed" to load (such as new and reflection, etc.)

candidate : [Loading occurs] The class file is loaded into the jvm through the "class loader". In order to prevent multiple copies of the same bytecode from appearing in the memory, the parent delegation mechanism is used (it will not try to load by itself) This class, but delegates the request to the parent loader to complete, in turn upwards)

candidate : [Loading rules] The native method classes in the JDK are generally loaded by the root loader (Bootstrp loader), and the extension classes implemented in the JDK are generally loaded by the extension loader (ExtClassLoader), while the class files in the program The loading is implemented by the system loader (AppClassLoader).

candidate : What it does at this stage of loading can be summarized as: find and load the binary data of the class, create an object of the java.lang.Class class in the JVM "heap", and store the class-related information in the JVM "Method Area"

Interviewer : Well...

candidate : After the step of "loading", the class file is now loaded into the JVM, and the corresponding Class object and class information are created and stored in the method area.

Candidate : What it does at this stage of "connection" can be summarized as: verifying the information of the class, allocating memory space for the "class variable" and assigning default values to it.

candidate : the connection can be refined into several steps: verification -> preparation -> analysis

candidate : 1. Verification: verify that the class conforms to the Java specification and JVM specification

candidate : 2. Preparation: Allocate memory for static variables of the class and initialize it to the initial value of the system

candidate : 3. Resolution: the process of converting symbolic references into direct references

Interviewer : Well...

candidate : After passing the "connection" step, the class information has now been verified and the memory space and default values have been allocated.

candidate : The next step is the "initialization" phase, which can be summarized as: assign correct initial values to static variables of the class.

candidate : The process is probably to collect class static variables, static code blocks, static methods to () methods, and then start execution from top to bottom.

candidate : If "Instantiate Object", the method will be called to initialize the instance variable and execute the code in the corresponding construction method.

Candidate : After pulling so much, now it is actually completed to the loading stage in (Compile -> Load -> Interpretation -> Execution), let's talk about what the [interpretation stage] did

Candidate : After the initialization is complete, when we try to execute a method of a class, we will find the bytecode information of the corresponding method, and then the interpreter will interpret the bytecode information into an instruction code that the system can recognize.

candidate : "Explain" what it does at this stage can be summarized as: convert bytecode into instructions recognized by the operating system

candidate : In the interpretation stage, there are two ways to interpret bytecode information into machine instruction codes, one is a bytecode interpreter and the other is a just-in-time compiler (JIT).

Candidate : JVM will compile the "hot code" and directly interpret the non-hot code. When the JVM finds that a method or code block is running particularly frequently, it may identify this part of the code as "hot code"

candidate : Use "hot spot detection" to detect whether it is a hot spot code. "Hot spot detection" generally has two methods, counter and sampling. HotSpot uses a "counter" method for detection. Two types of counters are prepared for each method: method call counter and back-end counter

candidate : Both counters have a certain threshold. When the counter exceeds the threshold and overflows, JIT compilation is triggered.

candidate : The just-in-time compiler saves the instruction code of the hot method, and the next time it is executed, there is no need to repeat the interpretation and directly execute the cached machine language

Interviewer : Well...

Candidate : After the interpretation phase is over, the execution phase is finally reached.

Candidate : What it does at this stage of "execution" can be summarized as: the operating system interprets the instruction code parsed by the interpreter and calls the system's hardware to execute the final program instructions.

candidate : The above is my understanding of the process from the source file (.java) to the code execution.

Interviewer : Well... I also want to ask about the parent delegation model you just mentioned...

candidate : definitely next time!

This article summarizes:

  • Java is cross-platform because the JVM shields the underlying operating system
  • The process from Java source code to execution can be summarized as four steps from the perspective of JVM: compile -> load -> explain -> execute

    • "Compilation" After syntax analysis, semantic analysis, and annotation processing, the class file is finally generated
    • "Loading" can be subdivided into steps: load->connection->initialization. Load loads the class file to the JVM, connects to verify the class information, allocates memory space and assigns default values, and initializes assigns the correct initial values to the variables. The connection can be refined into: verification, preparation, analysis
    • "Interpretation" is the conversion of bytecode into execution instructions that the operating system can recognize. There will be a bytecode interpreter and a just-in-time compiler in the JVM. During the interpretation, the code will be analyzed to see if it is "hot code", if it is "hot code", JIT compilation will be triggered, and there will be no need to repeat the interpretation when it is executed next time, which improves the speed of interpretation
    • "Execute" calls the hardware of the system to execute the final program instructions

Welcome to follow my WeChat public [16170b49524838 Java3y ] to talk about Java interviews. The online interviewer series is being updated continuously!

[Online Interviewer-Mobile] The series updated twice a week!

[Online Interviewer-Computer] The series updated twice a week!

Originality is not easy! ! Seek three links! !


Java3y
12.9k 声望9.2k 粉丝