Code Pumpkin

Types of Class Loader | Delegation Algorithm | JVM Internals

August 13, 2017
Posted by Pumpkin
Subscribe

In previous articles, we learned about JVM architecture and one of its component Class Loader SubSystem. In this article,  we will discuss about Types of Class Loaders and How Class Loader uses Parent delegation Algorithm to load the class. If you have not gone through previous article on JVM Internals, I would encourage you to read that first.

In interview room, we often come across the questions related to JVM architecture like,

  1. Which class loading mechanism or algorithm JVM uses to locate the class or
  2. What are the Types of Class Loaders JVM uses to load classes into Method area and so on.

You will find answer of all such Class Loader related questions in this article. Stay tuned.

The mechanism that enabled dynamic loading in Java is a class loader which is one of the cornerstones of Java dynamism. Class loaders are responsible for determining when and how classes can be added to a running Java environment, as well as making sure that important parts of the Java runtime environment are not replaced by impostor code.

You can also develop a custom class loader that is, capable of checking a digital signature before executing untrusted foreign code.

In this article, we will discuss about following

  1.  Types of Class Loader
  2.  How Class Loader works

Types of Class Loader

Class Loader Subsytem contains following three types of class Loaders

  1. Bootstrap Class Loader or primordial Class Loader
  2. Extension Class Loader
  3. Application Class Loader or System Class Loader

Bootstrap Class Loader

  • It is responsible to load core Java API classes i.e. the classes present in rt.jar, which is present at path (jdk\jre\lib\rt.jar) .
  • This location is called bootstrap class path i.e. Bootstrap Class Loader is responsible to load classes from bootstrap class path.
  • Bootstrap Class Loader is by default available with every JVM.
  • It is implemented in native languages like C / C++ and not implemented in java.

Extension Class Loader

  • It is the child class of Bootstrap Class Loader.
  • It is responsible to load classes from extension class path (jdk\jre\lib\ext)
  • It is implemented in java and the corresponding .class file is sun.misc.Launcher$ExtClassLoder.class

Application Class Loader / System Class Loader

  • It is the child class of Extension Class Loader.
  • This class loader is responsible to load classes from application class path.
  • It internally uses environment variable classpath.
  • It is implemented in java and the corresponding .class file name is sun.misc.Launcher$AppClassLoader.class

How Class Loader works

Types of Class Loader

  • ClassLoader follows delegation hierarchy algorithm. Whenever JVM come across  particular class, first it will check whether the corresponding .class file is loaded or not.
  • If it is already loaded in method area, then JVM will consider that loaded class.
  • If it is not loaded then JVM request class loader sub sytem to load that particular class.
  • Then class loader subsystem handovers the request to Application class loader.
  • Application class loader delegates the request to Extension class loader which in turn delegates the request to Bootstrap class loader.
  • Then Bootstrap class loader will search in Bootstrap class path If it is available then the corresponding .class will be loaded by Bootstrap class loader
  • If it is not available then Bootstrap class loader delegates the request to Extension class loader.
  • Extension class loader will search in Extension class path. If is is available, then it will be loaded otherwise extension class loader delegates the request to application class loader.
  • Application class loader will search in Application class path. If it is available, then it will be loaded otherwise we will get runtime exception saying ClassNotFoundException.

Important Point to Note:

  • Classes loaded by a child class loader have visibility into classes loaded by its parent class loaders. So classes loaded by System ClassLoader have visibility into classes loaded by Extensions and Bootstrap ClassLoader. 

That's all for this topic. If you guys have any suggestions or queries, feel free drop a comment. We would be happy to add that in our post. You can also contribute your articles by creating contributor account here

We will be including more articles in JVM Internals Series. Happy Learning 🙂 


Further Reading For interview Preparation

  1. JVM architecture Introduction
  2. Class Loader SubSystem
  3. Java Program to find from where java class is loaded
  4. interface VS abstract class
  5. Access Modifiers (public, private, protected and default scope)
  6. HashMap vs Hashtable Vs SynchronizedMap Vs ConcurrentHashMap
  7. CountDownLatch Vs CyclicBarrier
  8. How To Create Immutable Class In Java
  9. What is the use of private constructor in java?
  10. String Pool

Tags: , , , ,


Comments and Queries

If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code class="java"> 
String foo = "bar";
</code></pre>
For more information on supported HTML tags in disqus comment, click here.

Total Post : 80
Subscribe
Contribute Your Articles

Interview Experiences

Related Books

Like Us On Facebook

Alexa Page Rank