Pregunta

class Test {

}

public class NewClass {
    public static void main(String[] args) throws ClassNotFoundException {
        Test t = new Test();
        String s = new String("abc");
        Integer i = new Integer(1000);
        System.out.println(Class.forName("Test").getClassLoader());
        System.out.println(t.getClass().getClassLoader());
        System.out.println(NewClass.class.getClassLoader());
        System.out.println(java.lang.String.class.getClassLoader());
        System.out.println(java.lang.Integer.class.getClassLoader());
    }
}

Output:

sun.misc.Launcher$AppClassLoader@546b97fd
sun.misc.Launcher$AppClassLoader@546b97fd
sun.misc.Launcher$AppClassLoader@546b97fd
null
null

In the above code, why is the class loader for String and Integer null?

¿Fue útil?

Solución

From the JavaDoc of getClassLoader()

Returns the class loader for the class. Some implementations may use null to represent the bootstrap class loader. This method will return null in such implementations if this class was loaded by the bootstrap class loader.

Bootstrap class loader is responsible for loading class which is defined in Java API. String and Integer class are getting loaded by Bootstrap class loader.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top