JVM vs JDK vs JRE
TL;DR
- JVM (Java Virtual Machine): The engine that executes Java byte code. It provides platform independence (Write Once, Run Anywhere).
- JRE (Java Runtime Environment): JVM + Core Libraries. Everything you need to run a Java application.
- JDK (Java Development Kit): JRE + Development Tools (Compiler, Debugger). Everything you need to write and compile Java applications.
Concept
When you write a .java file, you need the compiler (javac) to turn it into a .class file (bytecode). The JDK contains this compiler.
If you give that .class file to your friend, they don’t need a compiler to run it. They just need the JRE, which provides the standard Java libraries (like java.lang.String) and the environment to execute the code.
Inside the JRE sits the JVM. The JVM is the actual machine process that reads the bytecode line by line, translates it into Windows/Mac/Linux specific machine code, and manages the RAM (via the Garbage Collector).
(Note: Starting with Java 11, Oracle stopped providing separate JRE downloads. Developers now usually bundle a custom, miniaturized runtime directly with their applications using jlink).
Examples
+-------------------------------------------------------------+
| JDK (Java Development Kit) |
| |
| +-------------------------------------------------------+ |
| | JRE (Java Runtime Environment) | |
| | | |
| | +-------------------------------------------------+ | |
| | | JVM (Java Virtual Machine) | | |
| | | - Just-In-Time (JIT) Compiler | | |
| | | - Garbage Collector | | |
| | | - Class Loader Subsystem | | |
| | +-------------------------------------------------+ | |
| | | |
| | +-------------------------------------------------+ | |
| | | Java Core Libraries | | |
| | | - java.lang, java.util, java.io | | |
| | +-------------------------------------------------+ | |
| +-------------------------------------------------------+ |
| |
| Development Tools: javac (Compiler), jdb (Debugger), javadoc|
+-------------------------------------------------------------+
Interview Questions
Q: Is the JVM platform-independent?
A: No. Java bytecode is platform-independent. The JVM itself is strictly platform-dependent. If you are on Windows, you must download the Windows JVM. If you are on Linux, you must download the Linux JVM. The JVM acts as a translator: it takes the universal bytecode and translates it into the specific machine code for the OS it is installed on.
Q: What is the JIT Compiler inside the JVM?
A: The JVM initially interprets bytecode line-by-line, which is slow. The Just-In-Time (JIT) Compiler runs constantly in the background, monitoring the application to see which methods are called frequently (Hot Spots). It compiles these specific bytecode methods directly into native machine code while the application is running. The next time that method is called, the JVM bypasses interpretation and executes the blazing-fast native machine code.