The JVM: Compilation & Bytecode in Java
There are two separate steps between writing a .java file and watching a program run. First, javac compiles source into bytecode β a compact, platform-neutral instruction set stored in .class files, which is not the same thing as real CPU instructions for any specific machine. Second, the JVM (Java Virtual Machine) loads that bytecode and actually executes it, using a Just-In-Time (JIT) compiler to turn frequently-run code paths into real machine code on the fly for speed.
This two-step split is exactly how "write once, run anywhere" works in practice: bytecode itself is completely platform-independent, so only the JVM implementation needs to be platform-specific. The same unmodified .class file runs on Windows, macOS, or Linux as long as each machine has a compatible JVM installed.
Source compilation
javac parses .java files, type-checks them, and emits .class files containing bytecode β a portable instruction set, not real machine code for any specific CPU.
Class loading
Running java Main tells the JVM's class loader to locate and load the needed .class files into memory.
Bytecode verification
Before executing anything, the JVM verifies the loaded bytecode is structurally valid and doesn't violate safety rules β a background safety step, not something you interact with directly.
Interpretation
The JVM starts out interpreting bytecode instruction by instruction, translating each one to real machine instructions as it goes.
JIT compilation
For code that runs repeatedly β hot loops, frequently-called methods β the JVM's Just-In-Time compiler compiles that bytecode directly into optimized native machine code, so it stops needing to be re-interpreted every single time.
public class Add { public static void main(String[] args) { int result = add(4, 7); System.out.println("Result: " + result); }Β static int add(int a, int b) { return a + b; }}Recommended resourcesAffiliate links Β· may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself β building beats watching.