Java Interview Prep Series — #5 Java Program Life Cycle: From Source Code to Execution
As a Java developer, you write code in .java files and run it with a simple command.
But what actually happens behind the scenes?
How does Java source code become something that the computer can execute?
In this article, we’ll understand the complete Java program life cycle, from writing the source code to compilation, bytecode generation, JVM execution, and finally the program output.
1. Overview of the Java Program Life Cycle
The complete Java program life cycle can be summarized as:
Java Source Code
↓
javac
↓
Bytecode
↓
.class File
↓
JVM
↓
Class Loading & Verification
↓
Interpreter / JIT Compiler
↓
Native Machine Code
↓
Execution
↓
OutputThe major stages are:
- Writing Source Code
- Compilation using
javac - Bytecode Generation
- Class Loading
- Bytecode Verification
- Bytecode Execution
- Interpreter / JIT Compilation
- Program Output
Let’s understand each step.
2. Step 1 — Writing the Source Code
The first step is writing Java source code.
You can use:
- IntelliJ IDEA
- Eclipse
- VS Code
- Any text editor
For example:
public class MyClass {
public static void display() {
System.out.println("Hello Java");
}
public static void main(String[] args) {
display();
}
}We save this file as:
MyClass.javaAt this stage, the file contains Java source code.
It is human-readable code and cannot be directly executed by the CPU.
The source code needs to be compiled before it can be executed by the JVM.
3. Step 2 — Compilation Using javac
The Java compiler is called:
javacThe javac compiler is provided by the JDK (Java Development Kit).
We can compile our program using:
javac MyClass.javaDuring compilation, the compiler:
- Reads the source code
- Checks the code for syntax and compile-time errors
- Performs compilation
- Generates Java bytecode
If the compilation is successful, a .class file is generated.
MyClass.java
↓
javac
↓
MyClass.class4. Step 3 — Bytecode
The generated .class file contains Java bytecode.
For example:
MyClass.classBytecode is an intermediate representation between Java source code and native machine code.
It is:
- Not Java source code
- Not native machine code
- Designed to be executed by the JVM
You can think of bytecode as a common intermediate language understood by JVM implementations.
5. Why Does Java Use Bytecode?
One of the biggest advantages of bytecode is platform independence.
Suppose you compile your Java program on Windows.
The compiler generates:
MyClass.classThat .class file contains bytecode.
You can then run the same bytecode on:
- Windows
- Linux
- macOS
provided there is a compatible JVM available for the target platform.
Conceptually:
MyClass.java
↓
javac
↓
MyClass.class
Bytecode
↓
┌──────────┼──────────┐
↓ ↓ ↓
JVM JVM JVM
Windows Linux macOS
↓ ↓ ↓
Execute Execute ExecuteThis is the foundation of Java’s famous principle:
Write Once, Run Anywhere (WORA)
Important Interview Point
Java source code is not directly platform-independent.
The bytecode is platform-independent, while the JVM implementation is platform-specific.
6. Step 4 — Running the Program
Once compilation is complete, we can run the program using:
java MyClassNotice that we don’t specify:
MyClass.classWe use:
java MyClassThe JVM starts the execution process.
7. Step 5 — Class Loading
When the JVM starts executing the application, it needs to load the required classes.
This is handled by the Class Loader.
The Class Loader loads the .class files into JVM memory when they are needed.
Conceptually:
MyClass.class
↓
Class Loader
↓
JVM MemoryThe JVM doesn’t necessarily load every class in the application at once.
Classes can be loaded as they are required.
8. Step 6 — Bytecode Verification
After the class is loaded, the JVM performs verification-related checks on the bytecode.
The purpose is to ensure that the bytecode is structurally valid and satisfies JVM constraints.
This helps protect the JVM from invalid or malformed bytecode.
Conceptually:
.class File
↓
Class Loader
↓
Bytecode Verification
↓
ExecutionThis verification step is one of the mechanisms that contributes to Java’s runtime safety.
9. Step 7 — JVM Executes the Bytecode
After the required classes are loaded and verified, the JVM starts executing the bytecode.
The JVM can execute bytecode using mechanisms such as:
- Interpretation
- Just-In-Time (JIT) compilation
Let’s understand both.
10. Interpreter
The JVM can initially execute bytecode using an Interpreter.
The interpreter reads bytecode instructions and executes them.
Conceptually:
Bytecode
↓
Interpreter
↓
Execute InstructionsThis allows the JVM to start executing code quickly without compiling everything into native machine code upfront.
Get engg_nirbhay’s stories in your inbox
Join Medium for free to get updates from this writer.
However, repeatedly interpreting frequently executed code can be less efficient.
This is where JIT compilation becomes important.
11. Step 8 — JIT Compilation
JIT stands for Just-In-Time Compiler.
The JIT compiler improves runtime performance by compiling frequently executed bytecode into native machine code.
Suppose a particular method is executed many times.
The JVM can identify it as frequently executed code and compile it into optimized native machine code.
Conceptually:
Bytecode
↓
Interpreter
↓
Frequently Executed Code
↓
JIT Compiler
↓
Native Machine Code
↓
Faster ExecutionThe native code generated by JIT is specific to the underlying platform.
This allows the JVM to optimize frequently executed code at runtime.
12. Interpreter vs JIT Compiler
Interpreter JIT Compiler Executes bytecode directly Compiles bytecode into native machine code. Allows quick startup Improves performance for frequently executed code. Executes instructions as needed Generates optimized native code. May be slower for repeatedly executed code. Can provide faster execution after compilation
The JVM can use both approaches during the execution of a Java application.
13. Complete Java Program Life Cycle
Let’s put everything together.
Step 1 — Write
MyClass.javaYou write Java source code.
Step 2 — Compile
javac MyClass.javaThe JDK compiler converts source code into bytecode.
Step 3 — Generate Bytecode
MyClass.classThe .class file contains Java bytecode.
Step 4 — Run
java MyClassThe JVM starts the execution process.
Step 5 — Load
The Class Loader loads the required classes.
Step 6 — Verify
The JVM verifies the loaded bytecode.
Step 7 — Execute
The Execution Engine executes the bytecode using interpretation and, where appropriate, JIT compilation.
Step 8 — Output
The program produces the expected result.
14. Complete Flow Diagram
The entire process can be visualized as:
Java Source Code
MyClass.java
↓
javac
↓
Java Bytecode
MyClass.class
↓
Class Loader
↓
Bytecode Verification
↓
JVM Execution
↓
┌──────────┴──────────┐
↓ ↓
Interpreter JIT Compiler
↓ ↓
└──────────┬──────────┘
↓
Native Execution
↓
Output15. Role of JDK, JRE and JVM
This process also helps us understand the relationship between JDK, JRE, and JVM.
JDK
The JDK provides development tools such as:
javacwhich is used to compile Java source code.
JRE
The JRE provides the runtime environment required to execute Java applications.
JVM
The JVM loads, verifies, and executes Java bytecode.
A simplified relationship is:
JDK
└── Runtime Components
└── JVMThe exact packaging of Java runtimes has evolved in modern Java distributions, but the conceptual distinction remains useful for understanding Java interviews.
16. Example: Running Hello Java
Let’s consider a simple program:
public class HelloJava {
public static void main(String[] args) {
System.out.println("Hello Java");
}
}Save it as:
HelloJava.javaCompile
javac HelloJava.javaThis generates:
HelloJava.classRun
java HelloJavaThe JVM loads the class, verifies it, executes the bytecode, and the output is:
Hello JavaSo the complete journey is:
HelloJava.java
↓
javac
↓
HelloJava.class
↓
Bytecode
↓
JVM
↓
Class Loading
↓
Verification
↓
Interpreter / JIT
↓
Execution
↓
Hello Java17. Common Interview Questions
Q1. What happens when we run javac MyClass.java?
The Java compiler compiles the source code and, if there are no compilation errors, generates a .class file containing Java bytecode.
Q2. What is stored inside a .class file?
A .class file contains Java bytecode along with class-related metadata and other information required by the JVM.
Q3. Is bytecode machine code?
No.
Bytecode is an intermediate representation designed to be executed by the JVM.
Q4. Why is Java platform-independent?
Java source code is compiled into platform-independent bytecode. A compatible JVM on the target platform executes that bytecode.
Q5. What happens when we run java MyClass?
The JVM starts the execution process, loads the required class, verifies it, and executes its bytecode using the JVM’s execution mechanisms.
Q6. Who compiles Java source code?
The Java compiler (javac), which is provided by the JDK.
Q7. What is the role of the JVM?
The JVM provides the runtime environment that loads, manages, and executes Java bytecode.
Q8. Does JVM always convert all bytecode into machine code immediately?
No.
The JVM can interpret bytecode and use JIT compilation for frequently executed code.
Q9. What is JIT?
JIT stands for Just-In-Time Compiler. It compiles frequently executed bytecode into native machine code at runtime to improve performance.
18. Quick Revision
Remember this simple flow:
.java
↓
javac
↓
.class
↓
Bytecode
↓
Class Loader
↓
Verification
↓
JVM
↓
Interpreter / JIT
↓
Native Execution
↓
OutputIn One Sentence
Java source code is compiled by
javacinto platform-independent bytecode, which is loaded and verified by the JVM and then executed through interpretation and/or JIT compilation.
Final Takeaway
A Java program doesn’t go directly from:
Java Code → CPUInstead, there is an important intermediate step:
Java Code
↓
Compiler
↓
Bytecode
↓
JVM
↓
Native ExecutionUnderstanding this life cycle is essential for Java developers because it explains the foundation of:
- Platform independence
- JVM execution
- JIT compilation
- Class loading
- Runtime performance
- Java application portability
Once you understand this flow, it becomes much easier to dive deeper into JVM internals, class loading, JIT compilation, memory management, and Garbage Collection.
Write Java code once, compile it into bytecode, and let the JVM handle the platform-specific execution.
Java Interview Prep Series #5 — Java Program Life Cycle





