Java: class8 class loading, link initialization stage three

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43336822/article/details/95599883

A load
1.java files are compiled into byte code class files, is byte code classes loaded from disk to memory.

2. Type the file marks the magic number

3. The system provides a class loader:

①Boostrap ClassLoader: boot class loader, loading the main jre / lib / re.jar, all jar package under the directory are required for the JVM is running a jar. ps class loader itself is actually a JAVA class, therefore, its own class loader needs to be loaded before using other class loader, obviously must have a top-level parent class loader (ie BootstrapClassLoader, the class loader is developed by the C.) is the parent of another class loader. So, if a class is a class loader BootstrapClassLoader, then the class getClassLoader () method returns null.

②Extension ClassLoader (Ext ClassLoader): extension class loader, the main core is loaded JAVA extension classes, namely jre / lib / jar file in the directory ext.

③ApplicationClassLoader (AppClassLoader): Application class loader, load is the main developers to write applications in a class, that is, all jar files in the CLASSPATH path.

4. parents work process delegated model are as follows:
(1) the current class loader check whether such has been loaded, if the original has been loaded already loaded class returns from his already loaded class.
(2) If not, go to delegate the parent class loader to load. Parent class loader will use the same strategy to see that they have been loaded class contains the class, returns have not commissioned the parent class to load until the delegate class loader to boot up. Because if the parent class loader is empty, on behalf of the use of the boot class loader as parent class loader to load. (That is, to see the String class loader is null)
(3) If the boot class loader fails to load, it will use the extension class loader to attempt to load, it will continue to fail to load AppClassLoader use, continue to fail is thrown an exception ClassNotFoundException.

5. Use the benefits of parent delegation model:
(1) security against user-written Java class dynamically replace some core classes. If the parents do not use loading delegation model for loading classes work, then we can use the custom classes at any time to dynamically replace core Java classes defined in the API. For example: if the hacker "virus code" custom implant to the String class among then the class loader from the String class definition is loaded onto the JVM, then the time will produce unexpected "viruses" on JVM. And this loading delegated parents can avoid this situation, because the String class has been at startup is carried out to load the bootstrap class loader.
(2) avoid repeated loading classes, because the JVM determines whether the two classes are the same class, it is determined according to whether or not the same name for the class, but also need to determine whether the class loader to load the class of a class loader is the same, the same the class file is a result of different class loader is obtained in two different classes.

The two classes are equal, two classes of the same class loader is the premise.

Second, the link stage
(1) Validation: the current byte code file meets the requirements of virtual machines (magic number, version number ......)
(2) Preparation: to open up the class variable memory area in the method, the initial value assigned types
(3 ) Analysis: parsing stage is a virtual machine to a constant pool of symbolic references to the process of replacing direct references.

Third, the initialization
to class variable assignment

Guess you like

Origin blog.csdn.net/qq_43336822/article/details/95599883