Top Java ClassLoader Interview Questions (2023) | CodeUsingJava
















Most frequently asked Java ClassLoader Interview Questions


  1. What is ClassLoader in Java?
  2. What are the types of ClassLoaders used by JVM ?
  3. What are the 2 rules of ClassLoader?
  4. What is Extension ClassLoader in Java?
  5. How do we create ClassLoaders?
  6. What is ClassLoader Hierarchy?
  7. What are the Methods of Java.lang.ClassLoader?
  8. What is the difference between Static ClassLoading and Dynamic ClassLoading?
  9. What are Java bytecodes?


What is ClassLoader in Java?

ClassLoader are used in loading other classes and interfaces in Java Virtual Machines.It is responsible in loading class files from the file systems, networks and other sources.

ClassLoader


What are the types of ClassLoaders used by JVM ?


ClassLoader

There are 3 types of ClassLoaders in Java Virtual machines:
  • BootStrap ClassLoader - helps by loading all the JDK Internal Classes.It also loads rt.jar and core classes such as java.lang.
  • Extension ClassLoader - used in loading classes from JDK extension directory.
  • Application ClassLoader - helps by loading class from current classpath and also in setting the classpath by invoking program by using -cp, -classpath Command Line Option, etc.

What are the 2 rules of ClassLoader?

Two Rules of ClassLoader are:
Visibility Principle - In this all the classes are loaded by parent ClassLoader that is visible to the child ClassLoader.
Delegation Principle - In this it delegates class loading requests to the parent ClassLoader before trying.

What is Extension ClassLoader in Java?

Extension ClassLoader is one of the 3 Default ClassLoaders that existed in Java Programming Language.It is a child of the BootStrap ClassLoader and helps by loading classes from the jre/lib/ext Directory.

ClassLoader


How do we create ClassLoaders?

We can create classloaders by using the following code:
public class Demo  
{  
public static void main(String args[])  
{  
System.out.println("How are you?");  
}  
}  


What is ClassLoader Hierarchy?

ClassLoader Hierarchy is used by loading a class into memory, then a request is raised for loading a class which delegates it to the Parent ClassLoader.This shows that uniqueness is maintained in the runtime environment.
We can also see the hierarchy by using the following command:
package com.codeusingjava.classloader;

public class ClassLoaderTest {

    public static void main(String[] args) {

        System.out.println("class loader for HashMap: "
                + java.util.HashMap.class.getClassLoader());
        System.out.println("class loader for DNSNameService: "
                + sun.net.spi.nameservice.dns.DNSNameService.class
                        .getClassLoader());
        System.out.println("class loader for this class: "
                + ClassLoaderTest.class.getClassLoader());

        System.out.println(com.mysql.jdbc.Blob.class.getClassLoader());

    }

}


ClassLoader


What are the Methods of Java.lang.ClassLoader?

loadClass - this method is used in loading all the classes that are referenced by the JVM.
defineClass() - this method is the final method that cannot be overriden.It is used in defining an array of bytes as an instance of the class.
findClass - this method is used in finding a specified class.
findLoadedClass - it is used in verifying if the class is referenced by JVM and was previously loaded or no.
Class.forName - this method helps in loading the class and also in initializing the class.It also gives an option to choose any of the ClassLoadera.

What is the difference between Static ClassLoading and Dynamic ClassLoading?

Static ClassLoading helps in statically loading new operators.
Dynamic ClassLoading invokes functions of the classloader at the runtime with the help of Claa.forName() Method.

What are Java bytecodes? Java bytecode is an Intermediate Language between Java that is the language by which the developer develops the program and the Machine Language that helps in running the program. When a Java program is compiled, then the output is a .class file that consists of bytecodes.