Saturday 24 March 2012

Java interview questions: - Elaborate the flow between bootstrap, extension and system class loader?

To understand this answer we will use below simple class lets get in to details of how the class will be loaded.

Import Koirala.Interview.Java;
public class mySimpleClass
{
public static void main(String[] args)
{
String myStr = "I am going to get a job";
System.out.println(myStr);
}}
Figure: - Flow between class loaders

The above class uses “String” class that means it has reference to “java.lang.String”.JVM will request the system class loader to load “java.lang.String”. But before he tries to load it will delegate to extension class loader. Extension class loader will pass it to Boot strap class loader. Now Boot Strap class loader does not have any parent so it will try to load “java.lang.String” using “rt.jar”. Now the Boot strap will return the class back using the same chain to the application.

Now lets see how “Import Koirala.Interview.Java;” is loaded using the class loaders. For the import statement JVM will make a call to the system class loader who will delegate the same to the extension class loader which will delegate the same to the boot strap class loader. Boot strap loader will not find “Koirala.Interview.Java” and return nothing to the extension class loader. Extension class loader will also check the same in its path and will not find anything thus returning nothing to the system class loader. System class loader will use its class path and load the class and return the same to the JVM who will then return it to the application.

See the following video on Java/J2EE Interview question which describes String Literal Pool: -



Click for more Java interview questions

Regards,

Visit for more author’s blog on Java interview questions

1 comment: