Before writing your first Java program, there is one important question to understand: what actually runs Java code? You will often hear three terms together — JDK, JRE, and JVM. They are closely related, but they are not the same thing.
A beginner can easily remember these names without understanding their relationship. A better approach is to understand what each one does and how they work together when you run a Java application.
Quick idea: The JDK is used to develop Java applications, the JRE provides the environment required to run Java applications, and the JVM is the engine that actually executes Java bytecode.
Why Does Java Need These Components?
Suppose you write a Java program containing this simple statement:
System.out.println("Hello Java");
The computer does not directly understand the Java source code you write. Java follows a process in which your source code is compiled into bytecode, and that bytecode is then executed by the JVM.
This separation is one of the major reasons Java became famous for its portability. Instead of creating completely different Java source code for Windows, Linux, and macOS, the same compiled bytecode can be executed by a JVM designed for each operating system.
Remember: Java source code is compiled into bytecode, and the JVM executes that bytecode.
What Is the JVM?
JVM stands for Java Virtual Machine. It is the component responsible for executing Java bytecode.
Think of the JVM as a translator and execution engine between Java bytecode and the operating system. The JVM understands Java bytecode and manages its execution on the particular machine where the Java program is running.
The important point is that the JVM is platform-dependent. A Windows system needs a JVM implementation designed for Windows, while Linux and macOS have their own JVM implementations.
How JVM Execution Works
Imagine that you create a file named Hello.java. After compilation, Java produces a bytecode file named Hello.class.
Hello.java
↓
Java Compiler
↓
Hello.class
↓
JVM
↓
Machine-level execution
The .class file contains Java bytecode. The JVM loads this bytecode, checks it, and executes it using the facilities provided by the underlying operating system and hardware.
What Is the JRE?
JRE stands for Java Runtime Environment. It provides the environment required to run Java applications.
The JRE contains the JVM along with the libraries and supporting files required by Java applications during execution.
JRE ├── JVM └── Java Runtime Libraries
A useful way to think about it is this: the JVM performs the actual execution, while the JRE provides the environment in which that execution can take place.
Important: The JRE is about running Java applications. It is not intended to provide the complete set of tools required to develop Java applications.
What Is the JDK?
JDK stands for Java Development Kit. It is the complete development environment used to create Java applications.
The JDK contains the runtime environment along with development tools. This means a developer can use the JDK to write, compile, debug, package, and run Java applications.
JDK
├── Development Tools
└── JRE
├── JVM
└── Java Runtime Libraries
For example, the Java compiler is one of the important development tools supplied with the JDK. The compiler converts Java source code into bytecode.
javac Hello.java
After compilation, you can run the generated class using the Java runtime:
java Hello
JDK vs JRE vs JVM
| Component | Full Form | Main Purpose | Contains |
|---|---|---|---|
| JDK | Java Development Kit | Develop Java applications | Development tools, runtime environment |
| JRE | Java Runtime Environment | Run Java applications | JVM and runtime libraries |
| JVM | Java Virtual Machine | Execute Java bytecode | Bytecode execution environment |
A Simple Real-World Analogy
Think about a restaurant.
- The JDK is like the complete restaurant setup, including the kitchen, equipment, chefs, and everything required to prepare food.
- The JRE is like the environment required to serve and operate the restaurant.
- The JVM is like the mechanism that actually performs the cooking operation.
The analogy is not technically exact, but it helps establish the relationship: JDK contains the tools needed for development, while the runtime side is responsible for executing the finished application.
JDK, JRE and JVM Relationship
JDK
│
▼
JRE
│
▼
JVM
│
▼
Executes Bytecode
This relationship is one of the most important things to remember when beginning Java.
Easy memory trick: JDK = Develop, JRE = Run, JVM = Execute.
What Happens When You Run a Java Program?
Let's follow the complete journey of a small Java program.
public class Hello {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}
First, you write the program in a source file with the .java extension.
The Java compiler supplied by the JDK then compiles the source code.
javac Hello.java
The compiler produces bytecode in a .class file.
Hello.class
When you execute the program, the JVM loads and executes that bytecode.
java Hello
Finally, the program produces the output:
Hello Java
Why Is Java Platform Independent?
This is one of the most common Java interview questions.
Java source code is compiled into bytecode rather than directly into operating-system-specific machine code. The bytecode can then be executed by a compatible JVM on different operating systems.
Java Source Code
↓
javac
↓
Java Bytecode
↓
┌─────┼─────┐
▼ ▼ ▼
JVM JVM JVM
Win Linux macOS
The Java program remains the same, while the JVM implementation changes according to the operating system.
Interview insight: Java is commonly described as platform independent because Java source code is compiled into platform-independent bytecode, which can run on different platforms through their respective JVM implementations.
Common Beginner Mistakes
- Thinking that JDK, JRE, and JVM are three completely separate technologies with no relationship.
- Thinking that the JVM directly understands Java source code. The JVM executes bytecode.
- Confusing the Java compiler with the JVM. The compiler converts source code into bytecode; the JVM executes that bytecode.
- Thinking that Java bytecode is the same thing as machine code. Bytecode is an intermediate instruction format designed for the Java runtime environment.
Interview Questions You Should Be Able to Answer
| Question | Key Answer |
|---|---|
| What is JDK? | A development kit containing tools required to develop Java applications. |
| What is JRE? | An environment containing the JVM and runtime libraries required to run Java applications. |
| What is JVM? | The virtual machine responsible for executing Java bytecode. |
| Which component compiles Java source code? | The Java compiler provided with the JDK. |
| What does the compiler produce? | Java bytecode, normally stored in a .class file. |
| Why is Java platform independent? | Because the same bytecode can run on compatible JVMs for different platforms. |
Quick Revision
| Concept | Remember This |
|---|---|
| JDK | Used by developers to create Java applications. |
| JRE | Provides the environment required to run Java applications. |
| JVM | Executes Java bytecode. |
| Compiler | Converts Java source code into bytecode. |
| Bytecode | Intermediate code stored in .class files. |
| Platform Independence | The same bytecode can run on different platforms through compatible JVMs. |
The easiest way to keep the entire concept in your mind is this: you use the JDK to develop Java programs, the JRE provides what is needed to run them, and the JVM performs the actual bytecode execution. Once this relationship becomes clear, the next steps — installing Java, configuring the environment, setting JAVA_HOME, and configuring PATH — become much easier to understand.
