Reread "in-depth understanding of Java Virtual machines" iv. How virtual machines load class files

Source: Internet
Author: User

1. Features of the Java language

After the Java code has been compiled into a class file (bytecode) by the compiler, it is necessary for the virtual machine to load it into memory and execute the code defined by the bytecode to implement the program development setting function.

Loading, joining (validating, preparing, parsing), initializing of types in the Java language are different from C + + when the program is running (c + + is required to connect at compile time), which also makes the Java language more flexible.

2, Virtual machine class loading process (1) when the virtual machine to load class (class loading time)

1) When a new, getstatic, putstatic, or invokestatic Four bytecode instruction is encountered, and the class is initialized without initialization

2) When using the reflection mechanism

3) Initializes the parent class of the class

4) Initialize the virtual machines to execute the main class

5) When using dynamic language features

That is, when an active reference is made to a class, initialization is performed, and the passive reference is not initialized.

Passive references are:

A. By referring to the parent class static field through the subclass, the subclass is not initialized;

B. Referencing an array of classes does not initialize the class

C. A class that defines a constant is not initialized when it is called, because the value of the constant is already stored in the constant pool of the calling class at compile time.

(2) The loading process of the class
1) Load

The loading process is mainly the process of loading the specified binary stream into memory (the method area) by a separate ClassLoader outside the virtual machine, that is, the storage structure of the class file translates into a run-time data structure within the memory method area and generates a corresponding class instance object.

2) Verification

After the binary byte stream is loaded into memory, it needs to verify the current file information and format, including file format validation, meta-information validation, bytecode verification, and symbolic reference verification.

3) Prepare

The class information that has been loaded into the memory method area is validated and then required to allocate a specified amount of memory space on memory and to automate initialization (setting the default values for each Type field). Static variables are allocated at this stage in memory.

4) parsing

Convert a symbolic reference from a constant pool definition in a class file to a direct reference, complete the layout of class information, fields, methods, etc. in memory

5) initialization

The process of executing a class constructor <clinit> method, the declaration initialization of a variable is performed at this stage.

3, the interface loading

The loading process of an interface differs from the loading process of a class, and the interface has its own initialization process, which is used to initialize the member variables defined in the interface.

Class is loaded before the parent class is loaded and then the child class is loaded, that is, to ensure that the class's parent class is loaded, but that the interface's loading does not require its parent interface to initialize, only the parent class is initialized when the parent interface is actually used.

Reread "in-depth understanding of Java Virtual machines" iv. How virtual machines load class files

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.