Detailed explanation of ClassLoader in java

Question: Calling String.class.getClassLoader() will return null. Why?

Bootstrap ClassLoader is written in C++.

Bootstrap ClassLoader is written by C/C++, it is part of the virtual machine itself, so it is not a JAVA class, that is, it cannot be referenced in the java code. When the JVM starts, it loads rt.jar through the Bootstrap class loader. The class files in the core jar package, int.class, String.class are all loaded by it. Then, as we have analyzed before, JVM initializes sun.misc.Launcher and creates Extension ClassLoader and AppClassLoader instances. And set ExtClassLoader as the parent loader of AppClassLoader. Bootstrap does not have a parent loader, but it can act as the parent loader of a ClassLoader. Such as ExtClassLoader. This can also explain the previous phenomenon of getting Null through the getParent method of ExtClassLoader.

What is a parental delegation?

Entrusted by the parents.
We finally came to this step.
When a class loader finds classes and resources, it uses the "delegation mode". It first determines whether the class has been loaded successfully. If not, it does not search by itself, but first goes through the parent loader, and then recursively. Continue until Bootstrap ClassLoader. If the Bootstrap classloader is found, it will return directly. If it is not found, it will return level by level, and finally reach itself to find these objects. This mechanism is called parent delegation.

Guess you like

Origin blog.csdn.net/qq_36428821/article/details/112548330