On this page (9 sections)
Java for Beginners · Module 1: Getting Started
🎯 Why Do JDK, JRE and JVM Confuse Everyone?
Think of a DVD. The disc holds the movie in a standard format. Any DVD player can play it, whether it is a cheap one or an expensive one. The movie does not change; only the player differs.
Java works the same way. Your compiled program is the disc (bytecode). The JVM is the player. Windows, Linux and macOS each have their own JVM, so the same disc plays on all of them. The JDK and JRE are just packages that bundle the player with other things.
1️⃣ JDK, JRE and JVM: What Is Inside What?
Figure 1: The JVM sits inside the JRE, and the JRE sits inside the JDK.
- JVM: the engine. It loads your bytecode, checks it is safe, and runs it.
- JRE: the JVM plus the standard class libraries (
String,Math,ArrayListand many more). It is enough to run a Java program. - JDK: the JRE plus developer tools, mainly the compiler
javac. You need it to write and compile programs.
2️⃣ From Source Code to Output
Java uses both a compiler and an interpreter. The compiler (javac) translates your code into bytecode once. The JVM then runs that bytecode on your particular computer.
Add.javasource code
Add.classbytecode, starts with CA FE BA BE
loads, verifies, then interprets and JIT-compiles
Figure 2: The journey of a Java program from source file to output.
Program 1: A small program to inspect
public class Add {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(7, 5);
System.out.println("7 + 5 = " + result);
}
}
7 + 5 = 12
- The method
addreturns the sum of twointvalues. maincalls it with 7 and 5 and prints the result.
3️⃣ A Look Inside Bytecode
The JDK includes a tool called javap that shows the bytecode inside a .class file. After compiling Add.java, run javap -c -p Add. This is the part for the add method:
static int add(int, int);
Code:
0: iload_0
1: iload_1
2: iadd
3: ireturn
iload_0andiload_1push the two parameters onto a stack.iaddtakes the top two numbers, adds them and pushes the answer.ireturnreturns the top value to the caller.
| Instruction | Stack after it | What happens |
| iload_0 | [ 7 ] | push parameter a (7) |
| iload_1 | [ 7, 5 ] | push parameter b (5) |
| iadd | [ 12 ] | pop two numbers, push their sum |
| ireturn | [ ] | pop 12 and return it to the caller |
Figure 3: How the JVM runs add(7, 5): the stack grows and shrinks with each instruction.
The real javap output also lists the constructor and main, which uses more instructions such as invokestatic (call a method) and getstatic (read System.out). You do not need to memorise any of this. The point is that bytecode is simple, neutral and the same on every operating system.
4️⃣ Proof: Every Class File Starts with CAFEBABE
Every .class file begins with the same four bytes, called the magic number. The JVM checks them first. If they are missing, it refuses to load the file. The next program reads those bytes from the file that javac made.
import java.io.FileInputStream;
import java.io.IOException;
public class MagicNumber {
public static void main(String[] args) throws IOException {
try (FileInputStream in = new FileInputStream("Add.class")) {
for (int i = 0; i < 4; i++) {
int b = in.read();
System.out.printf("%02X ", b);
}
System.out.println();
}
}
}
CA FE BA BE
FileInputStreamopens the file andin.read()reads one byte at a time.%02Xprints each byte as two hexadecimal digits.- The result
CA FE BA BEis the famous “CAFE BABE” marker of every Java class file. Compile and runAdd.javafirst soAdd.classexists in the same folder.
5️⃣ What “Run Anywhere” Really Means
- The bytecode (
.classfile) is the same everywhere. - The JVM is different for each operating system. Oracle, Eclipse Adoptium and others build one for each.
- So the promise is not “no software needed”. It is “no need to recompile for each system”.
Terms to remember
| Term | Meaning | Who uses it |
| JDK | Compiler plus tools plus runtime | Developers |
| JRE | Libraries plus JVM to run programs | Users (older packaging) |
| JVM | Engine that runs bytecode | Everyone, automatically |
| javac | Compiler: .java to .class | Developers |
| Bytecode | Neutral instructions in .class files | JVM |
| JIT | Turns hot bytecode into machine code while running | JVM |
| WORA | Write Once, Run Anywhere | The goal of Java |
Figure 4: Terms you will hear again and again.
⚠️ Common Confusions
| Wrong idea | Truth |
|---|---|
| “Java is only interpreted” | It is compiled to bytecode first, then run by the JVM |
| “Java is only compiled” | The JVM also interprets bytecode, and compiles hot parts with JIT |
| “The JVM is the same on every OS” | Each OS has its own JVM; the bytecode is what stays the same |
| “I need the JDK to run someone’s program” | Only a Java runtime is needed to run; the JDK is for developing |
| “The .class file is machine code” | It is bytecode, which only the JVM understands |
❓ Frequently Asked Questions
Q1. Is the JVM a program?
Yes. When you type java Add, you start a JVM process that runs your class.
Q2. Why did Java choose bytecode?
So one compiled file works on many systems, and so the JVM can check it for safety before running.
Q3. Is Java slower than C because of the JVM?
It can start slower, but the JIT compiler makes long-running Java programs fast. Lesson 4 explains how.
Q4. Can other languages run on the JVM?
Yes. Kotlin, Scala and Groovy also compile to bytecode.
✅ Key Takeaways
- JVM runs bytecode; JRE = JVM + libraries; JDK = JRE + developer tools.
javacturns.javainto.class(bytecode). The JVM then runs it.- Bytecode is the same on every OS; the JVM is specific to each OS.
- Every
.classfile starts withCA FE BA BE. - WORA means compile once, run anywhere there is a JVM.
🎓 Practice questions: (1) Draw the JDK, JRE and JVM as nested boxes. (2) What doesjavacproduce? (3) Why is a JVM needed on every computer? (4) Runjavap -c -pon your own small program and find the instruction that adds two numbers. (5) Explain “Write Once, Run Anywhere” using the DVD example.
All 30 lessons in this series (open course page)
Module 1 · Getting Started
- Lesson 1: What is Java? Features and Uses
- Lesson 2: Installing the JDK and Running Your First Program
- Lesson 3: JDK, JRE, JVM and WORA Explained
- Lesson 4: JVM Architecture and JIT Compilation
- Lesson 5: Hello World Program Explained Line by Line
Module 2 · Building Blocks
- Lesson 6: Comments, Identifiers and Keywords
- Lesson 7: Variables and Literals
- Lesson 8: Primitive Data Types in Java
- Lesson 9: Type Conversion and Casting
- Lesson 10: Taking Input with the Scanner Class
- Lesson 11: Escape Characters and Variable Scope in Java
Module 3 · Operators
- Lesson 12: Arithmetic Operators and Increment/Decrement in Java
- Lesson 13: Relational and Logical Operators in Java
- Lesson 14: Bitwise and Shift Operators in Java
- Lesson 15: Assignment, Ternary Operator and Operator Precedence in Java
Module 4 · Control Flow
- Lesson 16: if, if-else and else-if Ladder in Java
- Lesson 17: switch Statement in Java (Classic and Modern)
- Lesson 18: while and do-while Loops in Java
- Lesson 19: for Loop and Nested Loops (Patterns) in Java
- Lesson 20: break, continue and Labelled Loops in Java
Module 5 · Data and Methods
- Lesson 21: Arrays in Java (One-Dimensional)
- Lesson 22: Multidimensional Arrays in Java
- Lesson 23: Strings and the String Pool in Java
- Lesson 24: String Methods and StringBuilder in Java
- Lesson 25: Methods and Method Overloading in Java
- Lesson 26: Recursion Basics in Java
- Lesson 27: ArrayList and Wrapper Classes in Java
- Lesson 28: Exception Handling Basics in Java