Part 13 / Topic 5 of 8
A look under the hood: bytecode and the JIT
javac compiles Java source into bytecode — a compact set of instructions for the Java Virtual Machine, not for any real CPU. javap -c disassembles a compiled .class file back into readable bytecode, the same way dis does for Python.
static double applyGst(double amount) {
return amount * 1.18;
}
$ javac BytecodePeek.java
$ javap -c BytecodePeek
static double applyGst(double);
Code:
0: dload_0
1: ldc2_w #7 // double 1.18d
4: dmul
5: dreturn
dload_0 pushes the parameter onto an internal operand stack, ldc2_w pushes the constant 1.18, dmul pops both and multiplies, dreturn returns the result — a handful of simple instructions standing in for one line of Java.
The JVM starts by interpreting bytecode directly, one instruction at a time — simple, but slow. The JIT (just-in-time) compiler watches which methods run often enough to be worth the effort, then compiles those to real machine code on the fly, mid-run, replacing the interpreted version. This is why a hot method tends to get faster the longer a program runs, with no code change at all.
$ javac JitWarmup.java
$ java JitWarmup
== Timing 10 batches of 300000 calls each ==
batch 0: 29 ms
batch 1: 27 ms
...
batch 9: 27 ms
(sink, so the JIT cannot prove the loop is dead code and delete it: 7421976000000)
(varies by machine and run — compare this run's shape against -Xint in the README)
$ java -Xint JitWarmup
== Timing 10 batches of 300000 calls each ==
batch 0: 626 ms
batch 1: 649 ms
...
batch 9: 640 ms
(sink, so the JIT cannot prove the loop is dead code and delete it: 7421976000000)
(varies by machine and run — compare this run's shape against -Xint in the README)
With the JIT enabled (the default), 300,000 calls to a small, hot, side-effect-free method settle at around 27 ms a batch almost immediately — 300,000 calls is enough warm-up on its own for this method that the "before JIT" moment doesn't show up cleanly at this batch size. Forcing pure interpretation with -Xint (no JIT compilation at all) makes every batch take roughly 24 times longer, which is the real size of the gap this section is about — the JIT isn't a minor tweak, it's most of why long-running JVM code is fast at all.
🔍 Going deeper: none of this — bytecode shape, JIT thresholds, the interpreter — is something you write code to control. It's here so "why did the second run get faster" and "why does
javapshow instructions I never wrote" have real answers instead of mystery.
Full program: examples/BytecodePeek.java, examples/JitWarmup.java.
Done here? The practice set for Part 13 — including the solutions — is on the part page.