How Java Works: Compilation to Execution Process Explained
2 minute
How Java Works: Compilation β Execution Process
Understanding how Java works internally helps beginners and professionals write better, more efficient code. Java is unique because it is both compiled and interpreted, thanks to bytecode and the JVM.
Overview
Java programs go through two main stages before they run on your computer:
Compilation β converting your human-readable code into bytecode.
Execution β JVM reads the bytecode and converts it into machine code your computer can understand.
Unlike languages like C or C++, which compile directly into OS-specific machine code, Java adds an intermediate step (bytecode) that enables platform independence.
Step 1: Writing Java Code
You write your program in a file with a .java extension.
Example:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
This is called source code.
Humans can understand it, but computers cannot execute it directly.
Step 2: Compilation (Source Code β Bytecode)
You use the Java Compiler (javac) to compile your .java file:
javac HelloWorld.java
The compiler checks your code for syntax errors.
If everything is correct, it generates a .class file containing bytecode.
Key Points:
Bytecode is platform-independent.
JVM will later execute this bytecode on any operating system.
What Bytecode Looks Like
Bytecode is not human-readable, but itβs like a set of instructions for the JVM: