Java Interview Preparation JVM detailed study three (class loading mechanism)

Source: Internet
Author: User

Class loading process

After a class has been written and compiled into bytecode, it has seven stages to load into memory:

 Load =-(verify, prepare, parse) + = Initialize = Use = Uninstall


The three steps in parentheses can be consolidated into a " connect " step. The steps are not the end of a phase, but a phase begins. It's just that their starting phase basically follows this order (the parsing phase is more likely to occur at the time of use, with the purpose of dynamic binding), and these phases are mixed with each other, typically invoking or activating another phase during one phase of execution.

1. Loading

The "load" process is a phase of the "class loading" process, in which the virtual machine needs to complete the following three things at home in the stage:

1) define a binary stream of this class in the past by fully qualified name of a class

2) Convert the static storage structure represented by this byte stream into the data structure of the runtime of the method area

3) Generate a Java.lang.Class object representing this class in the Java heap as a portal to the data in the original class of the method area

The JVM does not specify detailed practices for these three steps, such as the source of the first binary stream is not deterministic, so many of the methods we have introduced to introduce classes are created:

        • From the zip package, the corresponding technology is the basis for the introduction of jar, EAR, and war package, the technical cornerstone of various frameworks introducing external packages
        • Obtained from the network, corresponding to the applet (a technology similar to JavaScript applications, technically not similar, sun company previously wanted it as a browser on the operating environment of the program)
        • Run-time compute generation, corresponding to dynamic agent technology
        • Generated by other files, corresponding to various applications in the JSP application

In particular, the connection phase has already started when the loading phase is not over.

2. Verification

The first step in verifying a connection is to ensure that the contained information in the byte stream of the class file conforms to the requirements of the current virtual machine and is not dangerous to the virtual machine.

The JVM standard does not provide a standardized step for this, but the general JVM implementation will have the following four processes:

1) file format validation to determine conformance to format specification

This phase verifies that the format is correct, that it conforms to the version requirements, that the encoding is UTF-8, etc.

2) meta-data validation, the semantic analysis of heap byte code

This stage verifies whether there is a parent class (except for objects that should have a parent class), whether a class that does not allow inheritance is inherited (such as a final decorated class), whether all methods in the parent class or interface are implemented , etc.

3) byte-code data validation, data flow and control flow analysis, is the most complex stage

Instances of this stage have types that match (such as declaring an int variable but putting in a long), jump instruction to the correct jump (to ensure proper execution of the Loop branch), and whether the type conversion is valid (the subclass object is assigned to the parent class and vice versa)

4) symbol Reference validation

For example, a symbol reference can find the corresponding class by the fully qualified name described by the string (JDBC uses a lot),

It is also important that the class, field, and Method Accessibility (Private\protected\public\default) can be accessed by the current class .

3. Preparation

This phase formally allocates memory for class variables (satic decorated parts) and sets class variable initial values, typically 0 values.

For example: public static int value = 1;

At this point the Prep phase prepares value in the method area, with a value of 0, which will be assigned to value in the initialization phase. 1. However, if it is final modified, this stage assigns a value to it.

4. Parsing

Replace a symbol reference in a constant pool with a direct reference procedure.

5. Initialization

Changes the value of the preparation stage to the value specified by the programmer.

The initialization phase is the process of executing the class constructor <clinit> () method , which is:

1) The compiler automatically collects the assignment actions of all class variables in the class and the combination of static statement blocks.

2) and is the first variable assignment, then the order of the static statement block (indeterminate)

3) and before the subclass's <clinit> () method is executed, the parent class must have finished executing its methods.

    Let's look at a program:

      

    Static classParent { Public Static intA = 1; Static{A= 2; }    }        Static classSubextendsParent { Public Static intDA; }     Public Static voidMain (string[] args) {System.out.println (sub.b); }

It is obvious that 2 should be output, because the first assignment in the parent is 1, and then it becomes 2 in static, and then the assignment in the subclass is performed.

But I'm skeptical about the order of 2 mentioned above, because the following code runs the result of 1.

    Static classParent {Static{A= 2; }         Public Static intA = 1; }        Static classSubextendsParent { Public Static intB =A; }     Public Static voidMain (string[] args) {System.out.println (sub.b); }
View Code

Therefore, the order of static code blocks and variable assignments for <clinit> (), as mentioned in the in-depth understanding of Java virtual machines, is not determined here, and the individual is tested on win8-64-bit machines (the HotSpot JVM) for the order of the Code, which the original author considers irrelevant.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.