类Loader reads the binary data in the class file into memory, stores it in the method area, and then creates a java.lang in the heap area. The .Class object is used to encapsulate the data structure of the class in the method area.
1, load: find and load the binary data of the class (load the information in the class file into the memory)
2, connect: merge the binary data of the class in the memory into the runtime environment of the virtual machine Medium (1) Verification: Ensure the correctness of the class being loaded. Including A, the structure check of the class file: check whether the fixed format of the Java class file is satisfied B, semantic check: ensure that the class itself conforms to the Java syntax specification C, bytecode verification: ensure that the bytecode stream can be secure by the Java virtual machine carried out. A bytecode stream is a sequence of opcodes. Each opcode is followed by one or more operands. Bytecode Check This step checks if each opcode is legal. D. Binary compatibility verification: Ensure that the classes referenced by each other are consistent. (2) Preparation: allocate memory for the static variables of the class and initialize it to the default value. (3) Parsing: convert the symbolic reference in the class into a direct reference (for example, the symbolic reference of the method, there is a method name and related descriptors) Composition, in the parsing phase, the JVM replaces the symbolic reference with a pointer, which is a direct reference that points to the memory location of the method in the method area of the class)
3, initialization: assigns static variables to the class The correct initial value. When the value to the right of the equal sign of a static variable is a constant expression, the static block is not called for initialization. Static initialization is called only if the value to the right of the equal sign is a runtime-operated value.
1, when a class loader receives a class load request, it does not first load the information of the class itself, but forwards the request to the parent class loader. Go up one by one. So all class load requests are passed to the parent class loader. Only if the class is not found in the parent class loader, the child class loader will try to load the class itself. A ClassNotFindException is thrown when the current class loader and all parent class loaders fail to load the class. 2. Significance: Improve the security of the system. User-defined class loaders are unlikely to load reliable classes that should be loaded by the parent loader. (For example, the user defines a malicious code, the custom class loader first lets the system loader load, the system loader checks that the code does not conform to the specification, so it does not continue to load) 3. Define the class loader: if some The class loader can load a class, then the class loader is called the definition class loader 4. The initial class loader: the definition class loader and all its child loaders are called initial class loaders. 5, the runtime package: (1) the class loaded by the same class loader and has the same package name constitutes the runtime package (2) only the class belonging to the same runtime package, can access the package visible (default) class and Class member. The effect is to restrict the user-defined class to impersonate the class of the core class library to access the visible members of the package of the core class library. 6, loading two copies of the same class object: A and B do not belong to the parent subclass loader relationship, and each load the same class.
1, full disk responsibility: When a class loader loads a class, other classes that the class depends on will also be loaded into memory by the class loader. 2, the cache mechanism: all Class objects will be cached, when the program needs to use a Class, the class loader first finds from the cache, can not find, only read data from the class file, converted into Class object, save Into the cache.
Two types of class loader
1, JVM comes with class loader (3 types): (1) Root class loader (Bootstrap):
a, C++, The programmer cannot get the class in the program b, responsible for loading the core library of the virtual machine, such as java.lang.Object c, does not inherit the ClassLoader class
(2) extension class loader (Extension):
a, written in Java, load the class library from the specified directory b, the parent loader is the root class loader c, is a subclass of ClassLoader d. If the user puts the created jar file into the specified directory, it will also be loaded by the extension loader.
(3) System Loader (System) or Application Loader (App):
a, written in Java b, the parent loader is an extended class loader c, load the class from the environment variable or class.path d, is the default parent loader loaded by the user-defined class e, is a subclass of ClassLoader
2, user-defined class loader: (1) subclass of Java.lang.ClassLoader class (2) user can customize the loading mode of the class (3) parent class loader Is the system loader (4) writing steps:
A, inherit ClassLoader B. Rewrite the findClass method. Load the class file from a specific location, get a byte array, and then use defineClass to convert the byte array into a Class object
(5) Why do you want to customize the class loader? A, can load the class file from the specified location, for example, from the database, the cloud class file B, encryption: Java code can be easily decompiled, so if you need to encrypt the code, then the encrypted code can not be used Java comes with the ClassLoader to load this class, you need to customize the ClassLoader, decrypt this class, and then load.
问题: There are several ways for a Java program to execute a class: 1. Active use (6 cases): The JVM must initialize each class when it is "first active".
(1) Create an instance of a class (2) Read and write static variables of a class or interface (3) Call the static method of the class (4) Get the class with the over-reflected API (Class.forName()) 5) Initialize a subclass of a class (6) When the JVM starts, the class that is marked with the startup class (the class that contains the Main method) // only when the static variable or static method used by the program is indeed defined in the class, Can be considered as active use of the class or interface. 2, passive use: In addition to the six cases of active use, other cases are passive use, will not lead to the initialization of the class. 3. The JVM specification allows the class loader to preload a class as it is expected to be used. If the class file is missing or has an error, the error is reported only when the program is "active for the first time". (Linkage Error error). If this class has not been "actively used" by the program, it will not report an error.
类Loading mechanism and interface: 1. When the Java virtual machine initializes a class, the interface implemented by the class will not be initialized. 2. When an interface is initialized, the parent interface of this interface will not be initialized. 3. The initialization of the interface is only caused when the program first uses the static variables of the interface.
ClassLoader: 1, call Classloader's loadClass method to load a class, not actively used, so the class will not be initialized. Uninstallation of
类: 1. Classes loaded by the three types of loaders (root, extension, system) that come with the JVM are never uninstalled. Because the JVM always references these class loaders, these class loaders use references to the classes they load, so these Class class objects are always reachable. 2. Classes loaded by user-defined class loaders can be unloaded.