The Java Virtual Machine (JVM) is the engine that enables Java programs to run on any platform. It acts as a runtime environment for Java bytecode, handling memory, execution, and platform independence.
It’s not hardware — it’s a virtual, abstract machine that runs Java applications.
🔍 JVM vs JRE vs JDK
When you install Java, you get:
- JDK (Java Development Kit): Tools for compiling and building Java applications (includes JRE).
- JRE (Java Runtime Environment): Everything needed to run Java applications (includes JVM).
- JVM: The part that actually runs your Java
.class
files.
JVM is platform-dependent (specific to OS), but Java is platform-independent because the same bytecode runs on any JVM.
🧠 Responsibilities of JVM
JVM handles the complete lifecycle of a Java application:
- Loads bytecode from compiled
.class
files - Verifies the bytecode for safety
- Executes the bytecode
- Manages memory and runtime resources
🏗️ Internal Architecture of JVM
The JVM is composed of several critical components:
1. ClassLoader
- Loads
.class
files at runtime - Divides loading into Bootstrap, Extension, and Application classloaders
2. Method Area
- Stores metadata about loaded classes (method info, constants, etc.)
3. Heap
- Runtime memory where all objects are stored
- Shared across all threads
4. Java Stack
- Each thread has its own stack
- Holds method frames, local variables, and partial results
5. Program Counter (PC) Register
- Stores the address of the current instruction being executed by the thread
6. Native Method Stack
- Holds native (non-Java) code, often interfacing with system libraries via JNI
7. Execution Engine
- Runs the bytecode using:
- A virtual processor
- An interpreter (line-by-line execution)
- A JIT (Just-In-Time) compiler for high performance (compiles hot code into native machine code)
✅ Quick Q&A
Q1. What is the difference between JDK and JRE?
JDK includes development tools + JRE. JRE only includes what’s needed to run Java apps.
Q2. Can Java source code be written in Notepad?
Yes.
Q3. Does Java create .class
files after compilation?
Yes.
Q4. Can one .java
file contain multiple classes?
Yes, but only one can be public.
Q5. What is a compilation unit?
It’s a single .java
file.
Q6. Is an empty .java
file valid?
Yes, it's syntactically valid.
JVM is what makes Java the "Write Once, Run Anywhere" language. Understanding its architecture is key to mastering Java performance and memory management.