Concept Explanation
These three acronyms confuse almost every beginner, and they appear in nearly every Java interview. The clean way to hold them is as nested layers:
+-------------------------------------------+
| JDK - Java Development Kit |
| javac, jar, javadoc, jdb (build tools) |
| +-------------------------------------+ |
| | JRE - Java Runtime Environment | |
| | core libraries (String, List, ...) | |
| | +-------------------------------+ | |
| | | JVM - Java Virtual Machine | | |
| | | executes bytecode | | |
| | +-------------------------------+ | |
| +-------------------------------------+ |
+-------------------------------------------+
- JVM — the engine. It reads
.classbytecode and executes it. This is the piece that makes Java portable: a different JVM ships for each operating system, but bytecode stays identical. - JRE — the JVM plus the standard library. Enough to run Java, not to compile it.
- JDK — the JRE plus developer tools, most importantly
javac. This is what you install to write Java.
One line to remember: you develop on the JDK, users run on the JRE, and the JVM does the actual work.
Why "Write Once, Run Anywhere" Works
Hello.java --javac--> Hello.class --> Windows JVM
(bytecode) --> macOS JVM
--> Linux JVM
You compile once. The bytecode is platform-neutral, and each platform's JVM translates it into native instructions at runtime.
Real World Example
A bank builds a payments service on its developers' laptops using the JDK. The compiled artifact is deployed into a Linux container that only needs a JRE. In production, the JVM inside that container runs the bytecode and handles memory via garbage collection.
Common Mistakes
- "I have Java installed" but
javacfails: You have a JRE. Install the JDK. - Thinking Java is interpreted like Python: Java is compiled to bytecode first, then executed by the JVM. Two steps, not one.
- Believing bytecode is machine code: Bytecode is for the JVM. The JVM produces machine code, often just-in-time.
Best Practices
- Ship the smallest runtime: Modern deployments use
jlinkto bundle only the modules the app needs. - Match versions: Bytecode compiled by JDK 21 will not run on a JVM 17. Compiling is forward-only.
- Learn the tools:
jar,javadoc, andjshellall ship with the JDK and are worth knowing.
Practice Exercises
- The Diagram: Redraw the three nested layers from memory and write one sentence describing each.
- The Bytecode Peek: Compile a class, then run
javap -c Welcometo disassemble it. You are reading real bytecode. - The Interview Answer: Write a three-sentence answer to "What is the difference between JDK, JRE, and JVM?" as if asked in an interview.