Java Interview QuestionsNo Comments

General Questions

1. How Java achieves Platform Independence?

When we compile a java file, we get a class file which contains bytecode. JVM converts bytecode to executable instructions as per the respective operating system. Executable instructions are different for each operating system. So, JVM is different for every operating system.

The platform dependency of JVM makes Java platform independent.

 

2. JDK vs JRE vs JVM

JVM makes Java portable. Virtual Machine which runs bytecode. Platform dependent. Contains Just In Time (JIT) Compiler.

JRE = JVM + Libraries + Components to run applications (java, javaw, rt.jar)

JDK = JRE + Compiler + Debugger [javac, javap, jar]

 

3. ClassLoaders in Java

ClassLoader is used by JVM to load the required classes. Order of invocation of classloaders by JVM:-

(a). System ClassLoader, loads all classes from Classpath. JVM first starts with System ClassLoader and tries to find classes from classpath(jar, war, ear etc)

(b). Extension ClassLoader, loads all classes from extension. If class is not found in System ClassLoader, JVM checks with Extension ClassLoader(jre, lib, ext directory)

(c). Bootstrap ClassLoader, loads all Java’s core files.

If still the class is not found then ClassNotFoundException gets thrown.