Member-only story
Is Java Compiled or Interpreted? Let’s Clear the Confusion!
When you’re just starting with Java, one question pops up often:
🤔 “Is Java a compiled language or an interpreted one?”
It’s a great question — and the truth is, Java is both! Let’s understand this step-by-step in a way that’s clear, simple, and beginner-friendly.
📌 What Happens When You Run a Java Program?
Let’s say you have the following Java code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
You run this using:
javac HelloWorld.java
java HelloWorld
What actually happens behind the scenes? Let’s break it down:
1️⃣ Compilation Phase (Source Code → Bytecode)
Java source files (.java
) are first compiled by the Java compiler (javac
) into something called bytecode (.class
files).
🧾 Bytecode is a low-level, platform-independent code that the JVM can understand — but your computer cannot run it directly.
javac HelloWorld.java → HelloWorld.class
✅ This is the compiled part of Java.
Bytecode is NOT machine code. It’s an intermediate representation.
2️⃣ Execution Phase (Bytecode → Machine Code)
The compiled bytecode is then passed to the Java Virtual Machine (JVM).
Here’s where Java becomes interpreted.
➡️ The JVM reads the bytecode line-by-line and translates it into machine-specific instructions that your OS can execute.
So yes — the JVM interprets the bytecode during runtime.
3️⃣ 🚀 JIT Compiler: Best of Both Worlds
To make execution faster, the JVM also uses a JIT (Just-In-Time) compiler.
🛠️ What does JIT do?
- It watches which parts of your code are used a lot.
- Then it compiles those parts into native machine code at runtime.
- So next time, the JVM doesn’t need to interpret them again — improving performance.