class loader

This article introduces class loaders from the following aspects:

1. Classes and class loaders

The two-parent delegation model

 

1. Classes and class loaders

       Class loader: Obtain the binary byte stream of a class through the fully qualified name of the class. This action is executed outside the virtual machine, which is convenient for the application to decide to obtain the required class. The code module that implements this action becomes the "class loader".

 

1. Classes and class loaders

     ① The "loading" phase of the class loading process requires the use of a class loader;

     ②For any class, the class loader that loads the class and the class itself need to uniquely determine its uniqueness in the virtual machine;

     ③ In layman's terms: whether two classes are equal is determined by the class loader and the class file ;

     ④ "Equal" here refers to: the return results of the equals(), isInstance(), isAssignableFrom() methods of the Class object, and also includes the instanceof relationship as the judgment of the object's ownership;

 

The two-parent delegation model

       

1. The perspective of the Java virtual machine

       There are two different class loaders:

       ① Bootstrap ClassLoader: C++ language implementation, part of the virtual machine itself;

       ②Other class loaders: Java language implementation, independent of the virtual machine, all inherited from the abstract class java.lang.ClassLoader

 

2. The developer's perspective

      There are three classes of class loaders:

     

      ① Bootstrap ClassLoader : Load the <JAVA_HOME>\lib directory, or the path specified by the -Xbootclasspath parameter, and load the class library recognized by the virtual machine into the memory of the virtual machine;

     

      ②Extension ClassLoader : Load all class libraries in the <JAVA_HOME>\lib\ext directory, or in the path specified by the java.ext.dirs system variable;

     

      ③Application ClassLoader (Application ClassLoader) : This class loader is implemented by getSystemClassLoader in ClassLoader, so it should not be the class loader of the system. Load the class library specified on the user path (classpath), and the user can use it directly. If there is no custom class loader in the application, the loader is the default class loader.

 

3. Class loader parent delegation model



 

(1) The hierarchical relationship between class loaders shown in the figure is called the Parent Delegation Model of class loaders;

(2) In the parent delegation model, the startup class loader does not have a parent class loader, and other class loaders have parent class loaders, and the relationship between the child and parent classes is not achieved through inheritance, but through the combination relationship. Reuse the code of the parent class loader ;

(3) The working process of the parental delegation model: when loading a class, the subclass loader first delegates the loading work to the parent class loader, which is the same for each level of class loader, and the loading request goes to the top-level startup class loader If the loading work still cannot be completed, the loader of the subclass will try to load the work;

(4) Using the parent delegation model to load classes, the obvious advantage is that the java class also has a priority hierarchy with its class loader. Such as: java.lang.Object, no matter what the environment is, there is a startup class loader to load, so the class is the same class in various class loader environments in the program

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326177956&siteId=291194637