GUESS THE ANSWER
class Student{}
public class Main{
public static void main(String ... args){
System.out.println(String.class.getClassLoader()+" "+ Student.class.getClassLoader());
System.out.println();
}
}
Options
1. ExtensionClassLoader@73d16e93
AppClassLoader@73d16e93
2. BootStrapLoader@73d16e93
AppClassLoader@73d16e93
3. null
AppClassLoader@73d16e93
4.BootStrapLoader@73d16e93
ExtensionClassLoader@73d16e93
ANSWER:
3. null AppClassLoader@73d16e93
Now this question is really hard, Lot's of you may not know what are those class Loader and what is happening in this question.
Let's fix that.
Class Loader: A class Loader is someone who load our classes and In JVM there are three types of class loader.
- Bootstrap Class Loader
- Extension Class Loader
- Application Class Loader
Bootstrap Class Loader is responsible for loading the classes in Bootstrap classpath(jdk/jre/lib). In this classpath there is rt.jar file which contains all the files of java.lang package so Object,String,StringBuffer,System,Integer,Math etc. all of them are loaded by Bootstrap ClassLoader.
Extension Class Loader is responsible for loading the classes in Extension classpath(jdk/jre/lib/ext). Extension classpath is inside bootstrap classpath in an ext folder. If there are any files in extension class path. It will be loaded by Extension classpath.
Application Class Loader is responsible for loading the classes in Application classpath. Now application class path is the environment variable we set while installing jdk for the first time.
> How Application Class Loader Works?
D:\Linkdeln\You dont know java Series\EP 6\Code\Student.class
This is where my .class file is located. It is not inside bootstrap or extension class path. It will loaded by application class loader. So, application class Loader is responsible for loading our defined classes.
Conclusion:
- String class is loaded by Bootstrap class loader.
- Student class is loaded by Application class Loader.
Answer should be : 2. BootStrapLoader@73d16e93
AppClassLoader@73d16e93
Here comes the twist
Bootstrap class Loader is not implemented in java it's a native(not implement in java) that's why it's returning null .
ApplicationClassLoader is implement in java that's why it's printing its object and calling it's toString method.
Final Answer:
3. null AppClassLoader@73d16e93