JIT compilation boundaries
What you will learn
A just-in-time compiler translates selected JVM instructions into native code while a program runs. HotSpot commonly combines interpretation, profiling and compiled execution. The JVM specification describes observable behavior, not a mandatory optimization strategy. A functional test is not a benchmark.
- Explain interpretation, JIT compilation and deoptimization.
- Separate semantic tests from performance measurements.
- Identify evidence needed for optimization claims.
Each example is a complete Main.java file. Use a fresh lab-owned directory: javac --release 21 -proc:none Main.java, then java Main. Use only trusted, locally compiled classes and fixed synthetic inputs. The runner enforces a process deadline.
Use a finite loop result
public class Main { static long square(long n) { return n * n; } public static void main(String[] args) throws Exception { long total = 0; for (int i = 1; i <= 1000; i++) total += square(i); System.out.println(total); }}Expected output:
333833500The checksum is meaningful regardless of execution mode; it does not imply square was compiled.
Runtime optimization is implementation-specific
HotSpot can collect execution profiles and compile frequently used methods or loops. Tiered compilation uses different compilation levels; inlining can remove call overhead, and escape analysis may enable scalar replacement of some allocations. Speculative optimizations can be abandoned when assumptions fail, transferring execution to a less optimized form. None guarantees that a given method will compile, an object will avoid allocation, or a program becomes faster after a fixed iteration count.
Observe without guessing
javap reports classfile instructions, not JIT output. On HotSpot, java -Xint Main is an interpreter-oriented diagnostic mode and -XX:+PrintCompilation reports compilation activity. The latter is not a stable machine-readable protocol and output depends on runtime and workload. Use only owned lab processes. A matching result in default and -Xint modes establishes functional consistency for those runs, not proof of JIT compilation or speed differences.
Measure separately
Benchmarking needs warmup, multiple measurement iterations, independent JVM forks, representative inputs and attention to dead-code elimination, constant folding and allocation effects. JMH can structure that work, but is an external dependency not executed here. Avoid ranking algorithms by a single nanoTime sample. Keep work bounded and record JDK, flags, hardware and measurement conditions when making a performance claim.
Observe polymorphic results
public class Main { interface Operation { int apply(int n); } static class Twice implements Operation { public int apply(int n) { return n * 2; } } static class PlusOne implements Operation { public int apply(int n) { return n + 1; } } static int apply(Operation op, int n) { return op.apply(n); } public static void main(String[] args) throws Exception { for (Operation op : new Operation[]{new Twice(), new PlusOne()}) { System.out.println(apply(op, 5)); } }}Expected output:
106Both implementations obey the interface. A VM may specialize and later revise call-site assumptions; no particular optimization is asserted.
Check a boundary
public class Main { static long sum(int count) { if (count < 0 || count > 10000) throw new IllegalArgumentException("count"); long result = 0; for (int i = 1; i <= count; i++) result += i; return result; } public static void main(String[] args) throws Exception { System.out.println(sum(0)); System.out.println(sum(100)); }}Expected output:
05050The boundary and ordinary input both produce exact results; this is correctness evidence, not elapsed-time evidence.
Common mistakes
Do not claim compilation after a fixed number of calls, equate bytecode size with performance or benchmark from one timer sample. Avoid tuning production JVM flags from a toy example.
Practice
Implement a bounded loop summing even integers from 2 through 200; print the checksum. Compare output in default and -Xint modes without timing either run.
Expected output:
10100Compare your answer with the manual practice criteria.
Official reading: VM structure · java launcher. Java 25 execution remains unverified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JIT compilation boundaries — knowledge check
Original JVM architecture questions.
What is JIT compilation?
What is deoptimization?
What does matching -Xint and default output prove?
Why use warmup and forks for benchmarks?
What can scalar replacement potentially do?