🖥️ This lecture covers how the Java Virtual Machine (JVM) works internally — its memory model, the difference between static and instance variables, how method calls use the Java Stack (frames), and how bytecode is executed on the operand stack.
At-a-glance Checklist
- [ ] Understand why Java uses a JVM instead of compiling directly to machine code
- [ ] Know the Java compilation pipeline:
.java → javac → .class (bytecode) → JVM
- [ ] Learn what JIT (Just-In-Time) compilation is and why modern JVMs use it
- [ ] Memorise the 3 main JVM memory areas: Method Area, Heap, Java Stack
- [ ] Understand what is stored in the Method Area (class info, static variables, methods)
- [ ] Understand what is stored on the Heap (object instances, instance variables)
- [ ] Understand what is stored on the Java Stack (frames for each method call)
- [ ] Know the difference between static and instance variables and where each is stored
- [ ] Trace through a method call chain using stack frames (push/pop)
- [ ] Walk through a bytecode execution example using the operand stack
- [ ] Understand how recursion creates new frames on the stack
- [ ] Know the
Thread.dumpStack() debugging command
Why Does Java Need a Virtual Machine?
The Problem with Direct Compilation
Languages like C and C++ compile source code directly into machine binaries that are specific to a particular hardware architecture (Windows, Mac, Linux, etc.).
Java's goal was to be a universal language — write once, run anywhere. This means Java code cannot be compiled all the way down to architecture-specific binaries.
The Solution: An Intermediate Layer
Java introduces the JVM as a middle layer: