What is JIT Compilation?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
JIT means just-in-time compilation. javac first compiles Java source into portable JVM bytecode. While the program is running, the JVM can compile frequently used bytecode into native machine code for the current processor.
HotSpot watches which methods and code paths run often. It can first use interpretation or a lower compilation tier, collect runtime information, and later compile hot code with stronger optimizations. Rarely used code may never reach the highest tier.
For example, a price-calculation method may run millions of times. Early calls include loading, profiling, and compilation work. Later calls can use optimized native code from the code cache. This is why the first call is not a good measure of steady-state performance.
The JIT can inline methods, remove repeated checks, and optimize loops. Some choices are based on runtime assumptions; if an assumption changes, HotSpot can deoptimize and continue safely. JIT is an implementation technique, not a requirement of the JVM specification, and its startup and memory costs mean performance should be measured with realistic workloads or JMH.
What is JIT Compilation
Direct Answer
Just-in-time compilation translates selected JVM bytecode into native machine code while the program runs. HotSpot profiles execution, compiles hot paths in tiers, applies runtime-informed optimizations, and can deoptimize compiled code when an assumption no longer holds.
Deep Dive & Architecture
Profile first, specialise later
A virtual call may initially see only one implementation. HotSpot can record that observation, inline the common target, and optimise the surrounding code as one path. This removes call overhead and exposes more constant folding or loop optimisation than the bytecode alone would justify.
Speculation needs a safe exit
Suppose a pricing loop has only RegularPrice objects during warm-up. The compiled path can be specialised for that target while retaining metadata needed to recover. If a new DiscountPrice class later appears and invalidates the assumption, execution deoptimises to a correct less-specialised state. The runtime can collect new profiles and compile again. Correctness never depends on the speculation remaining true.
Warm-up is part of the experiment
A short command-line program may finish before highly optimised code pays back its compilation cost. A long-running service can benefit after repeated traffic. Compilation threads, profiles, and compiled methods also consume CPU and code-cache space, so more compilation is not automatically better.
JIT logs and compiler flags describe one implementation, not portable application behaviour. For a real performance question, use a representative workload and a harness such as JMH, consume results so work cannot disappear, separate warm-up from measurement, and inspect diagnostics only after a repeatable difference exists.
Code Example
class JitCompilation {
static long triangular(int n) {
long total = 0;
for (int i = 0; i <= n; i++) total += i;
return total;
}
public static void main(String[] args) {
long result = 0;
for (int i = 0; i < 100_000; i++) result = triangular(100);
System.out.println(result);
}
}
The loop creates repeated execution; it does not prove which tier a particular JVM chose. Use implementation diagnostics or JMH for that question.
Check your understanding
Answer these without looking back at the article.
- What is the difference between javac and the JIT compiler?
- Why can optimized code be deoptimized?
- Why is one stopwatch loop a poor Java benchmark?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.