[In-depth research on Java] 2. JVM class loading mechanism, in-depth research on jvm

Source: Internet
Author: User

[In-depth research on Java] 2. JVM class loading mechanism, in-depth research on jvm

1. First, let's take a look at the Code Execution Process:

 

Ii. significance of research on class loading mechanism

It can be seen that class loading is the first step in Java program execution. Studying class loading helps you understand the JVM Execution Process and guides developers to take more effective measures to cooperate with the program.

The second purpose of studying the class loading mechanism is to enable the program to dynamically control class loading, such as hot deployment, to improve the flexibility and adaptability of the program. Iii. General Process principle of class loading: parent-parent delegation Mode 1. Search for the jre directory and jvm. dll, and initialize JVM; 2. Generate a Bootstrap Loader (start class Loader); 3. Bootstrap Loader automatically loads Extended Loader (standard extension class Loader ), set the parent Loader as Bootstrap Loader. 4. Bootstrap Loader automatically loads AppClass Loader (system class Loader) and sets its parent Loader as Extended Loader. 5. Finally, the HelloWorld class is loaded by AppClass Loader. Iv. Features of the Class Loader 1. When running a program, the specified class is always loaded by AppClass Loader (system class Loader. 2. When a class is loaded, each class loader will hand over the loading task to its parent. If the parent cannot be found, it will be loaded by itself.
3. Bootstrap Loader is the top class Loader and its parent Loader is null. 5. It is easy to obtain the class Loader. See the following example public class HelloWorld {
Public static void main (String [] args ){
HelloWorld hello = new HelloWorld ();
Class c = hello. getClass ();
ClassLoader loader = c. getClassLoader ();
System. out. println (loader );
System. out. println (loader. getParent ());
System. out. println (loader. getParent (). getParent ());
}
} Print the result: sun. misc. Launcher $ AppClassLoader @ 19821f
Sun. misc. Launcher $ ExtClassLoader @ addbf1
Null as shown in the preceding results, the parent Loader of ExtClassLoader is not obtained because Bootstrap Loader (started class Loader) is implemented in C language, if no definite method is found to return the parent Loader, null is returned. 6. There are three ways to load a Class: 1. When the command line starts an application, it is initialized and loaded by JVM. 2. It is loaded by Class. the forName () method is dynamically loaded. 3. The ClassLoader is used. the loadClass () method has a big difference in dynamic loading. You can see the following example:
Package zhongqiu. common. base; public class ClassLoadDemo {static {System. out. println ("ClassLoadDemo static initialization block is executed! ");} Public static void main (String [] args) throws ClassNotFoundException {ClassLoader loader2 = ClassLoadDemo. class. getClassLoader (); System. out. println (loader2); // use ClassLoader. loadClass () is used to load classes, and initialization blocks are not executed. // loader2.loadClass ("zhongqiu. test. test "); // use Class. forName () to load the Class. By default, the initialization block is executed. // Class. forName ("zhongqiu. test. test "); // use Class. forName () to load the Class and specify the ClassLoader. during initialization, no static block Class is executed. forName ("zhongqiu. test. test ", false, loader2 );}}

 

7. Custom ClassLoader

package zhongqiu.common.base.classload;import java.net.MalformedURLException;import java.net.URL;import java.net.URLClassLoader;public class MyClassLoader {@SuppressWarnings("resource")public static void main(String[] args)throws MalformedURLException, ClassNotFoundException, IllegalAccessException, InstantiationException {URL url = new URL("file:/D:/javaworkspace/JavaCommon/src/");ClassLoader myloader = new URLClassLoader(new URL[] { url });Class c = myloader.loadClass("zhongqiu.common.base.classload.Test");Test t3 = (Test) c.newInstance();}}

There is a ClassLoader class in the Java. lang package. The basic goal of ClassLoader is to provide services for class requests and dynamically load classes and resources as needed.
The Class Loader loads the class and initializes the class only when a class is to be used (The new Keyword is used to instantiate a class.
A Java application can use different types of class loaders. For example, in Web Application Server, Servlet loading and usage Development
Java. lang. String is using the JVM system Loader, Bootstrap Class Loader, and other classes defined by developers.
It is loaded by AppClassLoader. In JVM, the class name and the class loader distinguish different Java types. Therefore, JVM allows us to use different
The loader loads java classes with the same namespace. In fact, these java classes with the same namespace can be completely different classes. This
The mechanism ensures that the java. lang. String that comes with JDK is unique.

 

8,

Why is this dual-parent delegation used?

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.