search

Home  >  Q&A  >  body text

java - 所有类都是由Object类的记载器加载的吗?

看到网上这么说:

双亲委派机制描述
某个特定的类加载器在接到加载类的请求时,首先将加载任务委托给父类加载器,依次递归,如果父类加载器可以完成类加载任务,就成功返回;只有父类加载器无法完成此加载任务时,才自己去加载。

首先将加载任务委托给父类加载器,依次递归 这句话的意思是所有类都是由Object类的记载器加载的吗?

大家讲道理大家讲道理2802 days ago631

reply all(3)I'll reply

  • 大家讲道理

    大家讲道理2017-04-18 10:50:25

    The first class loader of the Java virtual machine is Bootstrap. This loader is nested in the Java virtual machine kernel. It is a binary code (not bytecode) written in C++.

    Using the delegation mechanism, the parent class will be searched recursively, that is, it is preferred to use Bootstrap to try to load, and if it cannot be found, go down. Prevent two copies of bytecode from appearing in memory.

    You misunderstood classes and classloader.

    When a class is recorded, first use the class loader of the current thread to load the first class in the thread. For example, this class is ClassA and the class loader is ClassLoaderA.

    If ClassA references ClassB, the system will use ClassLoaderA to load ClassB.

    Now there are 2 classes (Simplified version, actually more than 2 classes).

    We consider a situation where ClassX and ClassY have been loaded in memory, and they both refer to ClassZ. So who loads ClassZ?

    Obviously according to the loading steps described above, 2 copies of ClassZ will appear: ClassX is loaded once, and ClassY is loaded again. Because ClassY doesn't know that ClassX has been loaded.

    How to solve this problem is to search recursively to the parent class.

    The specific steps are to first search from BootstrapClassLoader. If BootstrapClassLoader has loaded this class, return it. If BootstrapClassLoader has not loaded this class, continue to search until it finds this class. If the class loader of this thread is not found, it means that the class has not been loaded, and the loader of the current thread is used to load it. You can use getContextClassLoader() to get the class loader of the current thread.

    reply
    0
  • 高洛峰

    高洛峰2017-04-18 10:50:25

    There are two types of class loaders in Java: system class loaders and user-defined class loaders.

    The system class loader will have loading path restrictions. For example, Bootstrap Class Loader在JDK1.6下,通过
    System.getProperty("sun.boot.class.path")can get the class loading path

    JAVA_HOME\jre6\lib\resources.jar;  
    JAVA_HOME\Java\jre6\lib\rt.jar;  
    JAVA_HOME\jre6\lib\sunrsasign.jar;  
    JAVA_HOME\jre6\lib\jsse.jar;JAVA_HOME\jre6\lib\jce.jar;  
    JAVA_HOME\jre6\lib\charsets.jar;  
    JAVA_HOME\jre6\lib\modules\jdk.boot.jar;  
    JAVA_HOME\jre6\classes 

    The classes in these paths are loaded by the next-level class loader in the corresponding path if the class file cannot be found. Bootstrap负责,其它路径下的class的递归到Bootstrap

    reply
    0
  • 迷茫

    迷茫2017-04-18 10:50:25

    What the person above said is right, I suggest you read JVM related books to learn more about it.

    reply
    0
  • Cancelreply