In-depth study of Java class loading mechanism

Source: Internet
Author: User

Directory

1. Why should you study Java class loading mechanism?

2. Understand the class loading mechanism, and what role do we have in project development?

3. Load implementation details.

4. Summary

First, why drug research Java class loading mechanism

Java class loading mechanism, so that we can use the custom class loader, in-depth understanding, to understand the compiler principle is also very helpful.

Second, understand the class loading mechanism, the application in the beginning of our project

In the project, we can use the custom class loader to help us do the following:

(1) encryption. Java code is easy to decompile, if you need to encrypt your code, the compiled code can be encrypted with some encryption algorithm, and then implement their own class loader, responsible for the encryption of the code to restore, to be recognized by the operating system. In this way, someone else sees an encrypted. class file that cannot be deserialized.

(2) Load from the specified source. If the bytecode file is not a standard way to load code and may be loaded from a database or a network, you need to customize the ClassLoader to load the class file from the specified path.

(3) performance. Dynamically create and execute code based on the actual situation.

Third, Java class loading mechanism principle

The flowchart of the Java program is executed as follows: Java source file (*.java) = = "Java compiler = =" bytecode file (*.class) = = "class loader = =" Byte code checker = = "Interpreter = =" Operating system, the entire file is loaded into the memory area, after a series of actions, Eventually form the code that the operating system recognizes, and the operating system finds the main method to begin execution. Where the heap (new comes in), stack (local variable), data segment (static variable or string constant), code segment (storing the codes).


As shown, public static void Main (String args[]) is compiled into the operating system and executes as the entry for the program. Then analyze the process.

As shown in the diagram on the left, the JRE provides a jar package that, as the name implies, provides the Java runtime environment. The picture on the right is the effect after the Hello World is compiled into a. class file. The process by which a Java class is loaded is as follows:

1. Execute javac Main.java with command, generate Main.class file, execute Java Main command, the JVM will load Main.class into memory and form a class object Main.class. For the differences between class objects and object objects, refer to Appendix I.

2. The JVM loads the Main.class into memory as follows: First, look for the JRE directory, locate the Jvm.dll, and initialize the JVM, and then generate a Bootstrap Loader (boot ClassLoader), Bootstrap Loader Auto-load Extended Loader (Standard extension class loader), and set its parent Loader to Bootstrap Loader; Bootstrap Loader automatically loads the Appclass loader (System class loader) and sets its parent loader to extended loader. Finally, the main class is loaded by Appclass loader.

namely: Bootstrap loader== "Extended loader==" Appclass Loader (*.class document description see appendix II)

3, then the second part, in the loading process, as described in the compilation principle, the following steps should be followed. The ClassLoader Appclass loader looks for the class's bytecode file Main.class, and constructs the object component represented within the JVM of the class.

(1) Loading, locating and importing class files.

(2) Link to merge binary data into the JRE.

(a) Verification: check the correctness of loading class file data

(b) Preparation: Allocating storage space for static variables of a class

(c) parsing: Turning a symbolic reference into a direct reference

(3) Initialize the static variables of the class, and initialize the static code block.

4. Common methods:

ClassLoader loader = Main. class . getClassLoader (); Loader.loadclass ("Main"), class loading in three ways:1, the command line when the application is started by the JVM initialization of loading 2, Dynamic loading via the Class.forName () method (3) , dynamically loaded via the Classloader.loadclass () method

Iv. Summary

Classes are loaded and eventually loaded into memory from the hard disk, and become executable based on the binary file stream that the operating system recognizes, so optimizing the code requires mastering the layout of the JVM in memory.

Appendix I: The difference between an object and class two classes

object is the parent class of all classes, and all classes, of course, include class classes. Therefore, the object class is the parent class of the class.

Private Class (ClassLoader loader) {    //  Initialize final field for ClassLoader.  The initialization value of Non-null    //  prevents future JIT optimizations from assuming this FINA L field is null.    ClassLoader = loader;}

Class, the constructor method is private. So the new class object cannot be displayed. It is used to describe the meta information of an object.

Appendix II: *.class Document Description

Java's class file is composed of 8 bytes of byte-based stream, which are arranged in strict order, and there is no gap between the bytes, for more than 8 bytes of data will be stored in the order of Big-endian, This means that high-level bytes are stored on low addresses, while low-bit bytes are stored on top of the address, which is also the key to the class file cross-platform.

In-depth study of Java class loading mechanism

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.