JVM, JRE, and JDK in Java
Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 4 min read
Understand how Java runs using JVM, JRE, JDK, bytecode, compiler, and platform independence.
JVM, JRE, and JDK are the three main parts of the Java environment. If you are learning Java for DSA, placement coding, or backend development, this topic is important because it explains what happens after you write a Java program and press the run button. A beginner should learn these three terms early because they answer three different questions: who executes Java code, what is needed to run Java code, and what is needed to develop Java code.
What Is JVM?
JVM stands for Java Virtual Machine. It is called a virtual machine because it behaves like a machine inside your real computer. Its main job is to execute Java bytecode after the source code has already been compiled.
The JVM also handles important runtime work such as loading classes, checking bytecode safety, managing memory, running the garbage collector, and using JIT compilation to improve performance. So, JVM is not just a simple runner; it is the engine that manages Java program execution. Different operating systems have different JVM implementations. Windows, Linux, and macOS each use a JVM built for that operating system, but all of them understand Java bytecode. This is what allows Java programs to move across platforms more easily.
What Is JRE?
JRE stands for Java Runtime Environment. The word runtime means the time when a program is actually running. So, JRE provides the environment required to run Java applications.
The JRE includes the JVM and the standard Java libraries. These libraries contain ready-made classes used by Java programs, such as classes for strings, input-output, files, collections, date and time, networking, and many other common tasks. If a person only wants to run an already compiled Java application, the JRE is enough. For example, a normal user who is not writing Java code only needs the runtime environment. But a programmer needs more than that because a programmer must write, compile, test, and debug Java programs.
What Is JDK?
JDK stands for Java Development Kit. It is the complete kit used by Java developers. If you want to create Java programs, you should install and use the JDK.
The JDK includes the JRE, and it also provides development tools. The most important tool is the Java compiler, called `javac`. The compiler converts `.java` source code into `.class` bytecode.
The JDK also contains other useful tools for development, debugging, documentation, packaging, and running Java applications. In simple words, the JDK is for developers, while the JRE is mainly for running programs.
Difference Between JVM, JRE, and JDK
- JVM is the engine that executes Java bytecode
- JRE is the environment that contains JVM and libraries needed to run Java programs
- JDK is the full development kit that contains JRE plus tools like the Java compiler
A simple way to remember the relationship is: JDK contains JRE, and JRE contains JVM. So, if you install the JDK, you already get the JRE and JVM with it. But if you only have the JRE, you can run Java programs, but you cannot compile new Java source code using `javac`.
How a Java Program Runs
First, the programmer writes source code in a file such as `Main.java`. This file contains human-readable Java code. Next, the compiler `javac` checks the source code. If there is no syntax error, it converts the code into bytecode and creates a file such as `Main.class`.
After that, the JVM loads the `.class` file, verifies the bytecode, prepares the required memory, and executes the program. The final result is shown as output on the screen. In short, the journey is: Java source code -> Java compiler -> bytecode -> JVM -> output.
```java
public class Main {
public static void main(String[] args) {
System.out.println("Java source code -> Bytecode -> JVM -> Output");
}
}
```
Bytecode and Platform Independence
Bytecode is an intermediate code generated by the Java compiler. It is not exactly source code, and it is not directly machine code for only one operating system. This bytecode layer is the main reason Java is called platform independent. Instead of creating a separate final program for every operating system, Java creates bytecode that can be understood by a suitable JVM.
This idea is commonly described as write once, run anywhere. It does not mean the JVM is the same on every operating system; it means the bytecode remains common while each operating system provides its own JVM.
JIT Compiler
JIT stands for Just-In-Time compiler. It is a part of the JVM that improves performance while the program is running. Normally, the JVM reads bytecode and executes it. But when some parts of the code are used again and again, the JIT compiler can convert those frequently used parts into native machine code. This makes Java programs faster during execution.
Common Beginner Confusion
Many students confuse JVM, JRE, and JDK because all three are connected. The easiest way to separate them is by asking the purpose: JVM executes, JRE runs, and JDK develops. If you are solving coding problems in the PrepCampus compiler or any Java compiler, the required JDK is already available in the background. That is why you can write Java code, compile it, run it, and see the output without installing anything on your own computer.
Why This Is Important
This topic is asked often in Java interviews because it checks whether you understand Java beyond syntax. A student who knows only `System.out.println` can write simple programs, but a student who understands JVM, JRE, and JDK understands how Java actually works behind the screen. For DSA, you do not need to manually work with JVM internals every day. Still, this foundation helps you understand compilation errors, runtime errors, memory usage, and why Java behaves differently from languages like C and C++.
Practice Question
Explain the complete journey of a Java program from `Main.java` source code to final output. In your answer, mention the role of `javac`, bytecode, JVM, JRE, and JDK.