Interview Q&A · Java · 4 questions in this topic

JVM, Bytecode & Class Loading — Java Interview Q&A

All Java topics

How source becomes bytecode, how the JVM executes it, and the difference between loading a class at compile time and finding it at runtime. Part of Core Java Basics · 294 questions in the Java bank.

What is the JIT compiler?

Answer

The Just-In-Time (JIT) compiler is part of the JVM that converts bytecode into native machine code while the program runs. Rather than compiling everything up front, it focuses on "hot" methods that execute frequently and optimises them using runtime profiling information, which lets Java approach native performance while keeping the portability of bytecode.

Key points

  1. 1HotSpot uses an interpreter for cold code and tiered JIT compilation for hot code.
  2. 2It can inline methods, remove dead code and unbox values based on observed runtime types.
  3. 3JIT-compiled code is discarded and recompiled as the program’s behaviour changes.

What is bytecode?

Answer

Bytecode is the instruction set stored in a compiled .class file. It is machine-independent and understood by the JVM rather than by the CPU directly. It is a compact, stack-based intermediate representation that the interpreter and JIT compiler execute.

Example

Example.javajava
javac Hello.java   # source  -> bytecode (Hello.class)
java  Hello        # bytecode -> execution by the JVM

When is ClassNotFoundException raised?

Answer

ClassNotFoundException is a checked exception thrown when the JVM cannot find a class that the running code explicitly asks for by its string name — for example Class.forName("com.example.Foo"), ClassLoader.loadClass(...), or when deserializing an object whose class is not on the classpath. It means the class could not be located at runtime even though the code compiled.

Example

Example.javajava
try {
    Class<?> c = Class.forName("com.example.Missing");
} catch (ClassNotFoundException e) {
    // class not on the classpath
}

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits