How Java Code Executes – Step by Step Guide with JVM, JRE, and Compilation Explained


Learn exactly how Java code is executed from writing source code to running it on JVM, including compilation, bytecode generation, and execution flow.

How Java Code Executes – Complete Detailed Tutorial

Java code execution is different from traditional compiled languages like C/C++. Java is a platform-independent, interpreted language, which means the code is first compiled into bytecode and then executed by JVM.

Understanding the execution process is crucial for both beginners and advanced Java developers.

1. Steps in Java Code Execution

Java code execution involves three major steps:

  1. Writing Java Source Code (.java)
  2. Compilation using JDK (javac) → Converts code to bytecode (.class)
  3. Execution by JVM (java) → JVM interprets or JIT compiles bytecode

2. Step 1 – Writing Java Source Code

  1. Write human-readable code in Java syntax
  2. File extension: .java
  3. Example:

public class Hello {
public static void main(String[] args) {
System.out.println("Hello, Java!");
}
}
  1. Save file as Hello.java

Explanation:

  1. Hello → Class name
  2. main() → Entry point
  3. System.out.println() → Print output

3. Step 2 – Compilation by JDK

  1. Use JDK's javac compiler to convert .java into bytecode.
  2. Command:

javac Hello.java
  1. Output: Hello.class (bytecode)

Why Bytecode?

  1. Bytecode is platform-independent
  2. JVM on any OS can interpret bytecode
  3. Enables WORA – Write Once, Run Anywhere

4. Step 3 – Execution by JVM

  1. JVM loads, verifies, and executes bytecode.
  2. Command:

java Hello
  1. Output:

Hello, Java!

JVM Execution Flow

  1. Class Loader: Loads .class file
  2. Bytecode Verifier: Ensures bytecode is safe and correct
  3. Interpreter / JIT Compiler: Executes bytecode as machine code
  4. Runtime Data Areas: Manages memory (Heap, Stack, Method Area)
  5. Garbage Collection: Automatically removes unused objects

5. Detailed Diagram of Java Code Execution


[Source Code: Hello.java]
|
| javac (Compiler)
v
[Bytecode: Hello.class] <-- Platform Independent
|
| java (JVM Execution)
v
[Machine Code executed by OS] <-- Platform Dependent
|
Output: Hello, Java!

6. Compilation vs Interpretation in Java

FeatureCompilationInterpretation
TooljavacJVM / Interpreter
Input.java file.class file (bytecode)
OutputBytecodeMachine code execution
SpeedFaster (compiled once)Slower (interpreted line by line)
PlatformPlatform-independent (bytecode)Platform-dependent (JVM)

Key Concept:

Java is both compiled and interpreted, using JIT compiler to speed up execution.

7. Command-Line Example of Execution

  1. Write code in Hello.java

public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
  1. Compile:

javac Hello.java
  1. Run:

java Hello
  1. Output:

Hello, World!

8. Advanced Note: JIT Compilation

  1. Just-In-Time (JIT) Compiler converts bytecode into native machine code during runtime
  2. Improves performance
  3. Frequently executed methods are compiled into machine code

9. Summary

  1. Java code execution = Source Code → Bytecode → JVM Execution
  2. Compilation produces platform-independent bytecode
  3. JVM converts bytecode to machine code
  4. Ensures security, portability, and performance

Key Points to Remember:

  1. javac → Compile → .class bytecode
  2. java → JVM → Execute bytecode
  3. JVM interprets or JIT compiles code
  4. Platform-independent + secure + robust