Java is known for its platform independence and robust performance, all thanks to its internal working mechanism. Let's walk through the complete Java execution cycle, from writing code to executing it on your device.
๐งญ Step-by-Step Journey of a Java Program
๐ 1. Writing the Code
You begin by writing Java code in a file with the .java
extension.
๐งฎ 2. Compilation (javac)
-
The Java compiler (
javac
) translates your source code into bytecode. -
Output is a
.class
file (not human-readable).
๐งพ Output → HelloWorld.class
๐ฆ 3. Bytecode
-
Bytecode is platform-independent code.
-
Cannot be run directly by your operating system.
-
Acts as an intermediate layer between source and machine code.
๐ง 4. Class Loader (JVM Component)
-
Loads the
.class
files (bytecode) into memory. -
Loads built-in Java libraries as needed.
-
Performs linking and initialization.
๐ 5. Bytecode Verifier
-
Ensures code follows Java security rules.
-
Verifies:
-
No memory violations
-
No illegal access
-
No stack overflow
-
⚙️ 6. Interpreter & JIT Compiler
Java uses both an interpreter and a Just-In-Time (JIT) compiler:
Component | Role |
---|---|
๐งพ Interpreter | Executes bytecode line by line (slower for large apps) |
⚡ JIT Compiler | Compiles frequently used bytecode to native machine code for faster execution |
๐ JIT makes Java programs faster at runtime by converting “hot” code paths into machine code.
♻️ 7. Garbage Collector (GC)
-
Automatically cleans up unused memory.
-
No need for
free()
or manual deletion. -
Helps avoid memory leaks and crashes.
๐ Java Internal Workflow (Visual Text Diagram)
๐ก Real-World Analogy
Writing Java code is like writing a recipe in English (source code).
The compiler translates it to a universal cooking language (bytecode).
JVM is the skilled chef who interprets it and cooks it in any kitchen (OS), ensuring quality (security) and cleaning up afterward (GC).
๐งพ Summary Table
Step | Description |
---|---|
Write Code | Create .java file |
Compile | Convert to .class bytecode |
Load | Class loader loads classes into memory |
Verify | Bytecode verifier checks safety |
Execute | JVM interprets or compiles it |
Cleanup | GC handles memory management |