Many people have trouble answering JVM interview questions. This is due to a couple of reasons. Firstly, one could be an experienced Java programmer, programming for years, without knowing the internals of JVM. Secondly, one usually deals with JVM internals only when there are some performance issues, or if the job role or the company product necessitates dealing with JVM internals.
Basic understanding of JVM internals, memory management, garbage collection etc. is absolutely necessary for being successful in any Java interview. Every company at faces performance and memory issues with their systems, and hence questions on JVM internals is usually asked to gauze your experience with performance.
Below JVM interview questions, answers, tips and samples will simplify this topic and get you grounded on the basic JVM concepts; which is sufficient for most interviews.
Important keywords are provided at the end of the questions. Review them, make them a part of Java JVM vocabulary, and talk about them confidently during your interview process.
JVM - JVM, which stands for Java Virtual Machine, is a virtual machine that understands and runs java bytecodes.
When you compile a java program, the output is a .class file which contains bytecodes. JVM understands bytecodes and can execute the .class Java files.
There are specific implementations of the JVM for specific platforms - Windows, Linux etc. The same Java .class file can be run for any of the JVM implementations. This is how Java programming language is platform independent and make the Java programs portable i.e write once, run anywhere.
JRE - JRE, which stands for Java Runtime Environment, provides an implementation of the JVM, supporting libraries and other components required to run Java programs. JRE also provides components that enable two kinds of deployment of Java programs. Java plug-in, which enables java programs to run on browsers and Java Web Start, which deploys standalone java applications.
JDK - JDK, which stands for Java Development Kit, contains the JRE plus tools such as compilers, debuggers etc. which are required for developers to develop Java programs.
When you compile a Java program, the output is a .class file which contains bytecodes that are machine and platform independent. JVM understands the bytecode and runs Java programs. Specific JVMs are implemented for specific platforms. The same .class file can run on any JVM implemented on any platform and machine. So you write a Java program once, compile it once, and run it in any platform.
Java bytecode is an intermediate language between Java which is the language in which the developer develops the program, and the machine language which runs the program. When a Java program is compiled, the output is a .class file which contains bytecodes. JVM loads the Java classes through class loader and executes them.
Java program (.java files) are compiled into bytecodes (.class files) using the Java compiler (javac). A class loader, which is a component of the JVM loads the compiled Java bytecodes into specific areas called runtime data areas. Java execution engine, which is also a component of the JVM executes the Java bytecodes.
Java class loaders are components in JVM that load Java class file at runtime, when they are referenced for the first time. Each class loader has its own specific namespace, in which it stores the classes that it loads.
Following are the key characteristics of Java class loaders
Hierarchy - Java class loaders in JVM are hierarchal with a parent-child relationship. Bootstrap class loader is the parent of all class loaders.
Delegation model - Before a class loader loads a class, it checks if its parent class loader has already loaded that class. If the parent class loader has loaded that class, then that class is used. If the parent class loader has not loaded the class, then the child class loader loads the class and uses it.
Visibility - ...
*** See complete answer in the Java Interview Guide
There are four main Java class loaders in the JVM. Bootstrap class loader, Extension class loader, System class loader, User defined class loader
Bootstrap class loader - Bootstrap class loader is the parent class loader in the class loader hierarchy in JVM. Bootstrap class loader loads Java API classes.
Extension class loader - ...
*** See complete answer in the Java Interview Guide
Following are the key steps by which a class is loaded and initialized by the JVM
Load - The class file is loaded to JVM memory.
Verify - Verifies that...
*** See complete answer in the Java Interview Guide
Runtime data areas are memory areas assigned when the JVM runs on the OS. Runtime data areas are of two kinds - Those that are created for each thread and those that are shared by all threads...
*** See complete answer in the Java Interview Guide
Java class files or bytecodes that are loaded to the runtime data areas are executed by the execution engine, which is a component of the JVM. Before executing, the execution engine reads and transforms the bytecodes into ...
*** See complete answer in the Java Interview Guide