简体   繁体   中英

Which classloader loades Array class in Java?

The following code prints the classloader of 4 arrays.
My question is:

  1. Why classloader of array is different?
  2. Does output null have the same meaning?
/**
 * output:
 * null
 * sun.misc.Launcher$AppClassLoader@18b4aac2
 * null
 * null
 */
public class Test {
    public static void main(String[] args) {
        String[] strings = new String[2];
        System.out.println(strings.getClass().getClassLoader()); // ①

        Test[] tests = new Test[2];
        System.out.println(tests.getClass().getClassLoader()); // ②

        int[] ints = new int[2];
        System.out.println(ints.getClass().getClassLoader()); // ③

        Integer[] integers = new Integer[2];
        System.out.println(integers.getClass().getClassLoader()); // ④
    }
}

Null represents the bootstrap class loader here. See:

https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getClassLoader

A minimal subset of the core JDK classes are loaded using the boot class loader, which is represented as null .

Other classes are then loaded with the AppClassLoader . For more info check out this answer.

Test[] tests = new Test[2];

This is your own class. It was loaded by Launcher$AppClassLoader .

But others, they belong to the java.lang.* package. They were loaded by bootstrapClassLoader .

System.out.println(System.getProperty("sun.boot.class.path"));

You can try it; it can get all paths by BootStrapClassLoader .

The main purpose of the classloader is to return the class loader for the class which loads classes into memory dynamically.

Now, answer to your questions are:

  1. Why the classloader of the array is different?
    Ans: Because it is being called by the different classloaders and not all classes are being loaded by a single ClassLoader .
  2. Does output null have the same meaning?
    Ans: Null means this class was loaded by the bootstrap class loader which means a primitive type or void . And A Bootstrap Classloader is a Machine code that kickstarts the operation when the JVM calls it . It is not a java class. Its job is to load the first pure Java ClassLoader . Bootstrap ClassLoader loads classes from the location rt.jar. Bootstrap ClassLoader doesn't have any parent ClassLoaders. It is also called as the Primordial ClassLoader.

After reading JVM specification and doc of ClassLoader.java , I found the answer.

The output null have different meaning.

  • output of ① null means boostrap class loader
  • output of ③ null means no class loader
  • output of ④ null means boostrap class loader

From doc of ClassLoader.java :
Class objects for array classes are not created by class loaders, but are created automatically as required by the Java runtime. The class loader for an array class, as returned by Class.getClassLoader() is the same as the class loader for its element type; if the element type is a primitive type, then the array class has no class loader .

From JVM specification 5.3 :
If C is not an array class, it is created by loading a binary representation of C using a class loader. Array classes do not have an external binary representation; they are created by the Java Virtual Machine rather than by a class loader .

From JVM specification 5.3.3 :

  1. If the component type is a reference type, the algorithm of this section is applied recursively using class loader L in order to load and thereby create the component type of C.
  2. The Java Virtual Machine creates a new array class with the indicated component type and number of dimensions.
    If the component type is a reference type, C is marked as having been defined by the defining class loader of the component type. Otherwise, C is marked as having been defined by the bootstrap class loader .
    In any case, the Java Virtual Machine then records that L is an initiating loader for C.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM