What are the different types of class loaders in Java?

Class Loaders are JVM components responsible for dynamically loading Java classes into memory at runtime. They follow a parent delegation model, where a class loader first delegates the loading request to its parent before attempting to load the class itself. This mechanism improves security, avoids duplicate class loading, and ensures consistent class management across applications.

Key Points: • Bootstrap ClassLoader loads core Java classes such as java.lang, java.util, and other fundamental JDK classes. • Platform ClassLoader (called Extension ClassLoader before Java 9) loads JDK platform libraries and modules that extend the core Java functionality. • Application ClassLoader, also known as the System ClassLoader, loads classes from the application's classpath, including user-defined classes and external libraries.

Example: When a Java application starts, classes like String and Object are loaded by the Bootstrap ClassLoader. JDK platform modules are loaded by the Platform ClassLoader, while application-specific classes such as Employee or CustomerService are loaded by the Application ClassLoader.

Code Example:

public class ClassLoaderDemo {

    public static void main(String[] args) {

        System.out.println(
            String.class.getClassLoader());

        System.out.println(
            ClassLoaderDemo.class.getClassLoader());
    }
}

Output: null jdk.internal.loader.ClassLoaders$AppClassLoader

Interview Tip: A concise interview answer is: Java primarily uses three class loaders: Bootstrap ClassLoader, Platform (Extension) ClassLoader, and Application (System) ClassLoader. They work in a hierarchical parent delegation model where core JDK classes are loaded first, followed by platform libraries and finally application-specific classes.