Java Virtual Machine
1. Beginner Mental Model
Think of JVM as a translator + manager between your program and the real computer. You give it bytecode (.class), it gives the OS native instructions, while handling memory + safety.
Hello.java --javac--> Hello.class (bytecode) --JVM--> runs on Windows/Mac/Linux
JDK vs JRE vs JVM: JDK = tools to develop (javac, java), JRE = runtime to run (JVM + libs), JVM = engine that runs bytecode.
2. Three Big Parts (exam diagram)
| Part | Job | Beginner analogy |
|---|---|---|
| Class Loader | loads .class, verifies safety, links |
airport security check |
| Runtime Memory | Method area (class info), Heap (objects via new), Stack (method calls/locals), PC register |
different rooms for different things |
| Execution Engine | Interpreter (line-by-line) + JIT (hot code → fast native) + GC (frees unused objects) | slow careful reader + fast memorizer + cleaner |
3. Flow with Code
public class Demo {
public static void main(String[] args) {
String s = new String("hi"); // object → Heap, variable s → Stack
System.out.println(s);
}
}
Try: System.gc() hints GC (not guaranteed). Deep recursion → StackOverflowError. Too many objects → OutOfMemoryError: Java heap space.
4. Why WORA Works
Same Hello.class runs on any JVM (Windows x86, Mac ARM) because each JVM translates to its own native code. JVM itself is platform-dependent, your program is not.
5. Exam Points
- Draw JVM architecture (loader, memory, engine) — 7 marks, very frequent.
- JDK vs JRE vs JVM — 3-5 marks, very frequent.
- Interpreter vs JIT vs GC roles — 5 marks, frequent.