Example Program (Java)
class Demo {
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = a + b;
[Link](c);
}
}
PHASE 1: Source Code → Bytecode
Step 1: Writing Source Code
Programmer writes code in high-level language (Java).
Saved as: [Link]
This code is human-readable.
Step 2: Compilation by Java Compiler (javac)
Command:
javac [Link]
What happens internally:
1. Lexical Analysis (tokens generated)
2. Syntax Analysis (grammar check)
3. Semantic Analysis (type checking)
4. Code Generation (bytecode creation)
Output file:
[Link]
This file contains Bytecode, not machine code.
Bytecode
If we disassemble using:
javap -c Demo
We may see:
iconst_5
istore_1
iconst_10
istore_2
iload_1
iload_2
iadd
istore_3
getstatic
invokevirtual
return //These are JVM bytecode instructions.
PHASE 2: Bytecode → Machine Code (Inside JVM) Now execution
[Link]:java Demo
Step 3: Class Loader
JVM loads [Link] into memory.
Verifies bytecode (security check).
Step 4: Bytecode Verifier
Checks:
Type safety
Stack safety
Illegal memory access
If safe → execution continues.
Step 5: Interpreter Execution
JVM interpreter:
For each instruction:
1. Fetch bytecode instruction
2. Decode
3. Execute
Example:
iconst_5
Pushes constant 5 onto operand stack.
iadd
Pops two values from stack
Adds them
Pushes result back
Step 6: JIT Compilation (Optimization)
Modern JVM uses JIT (Just-In-Time compiler).
If some code runs frequently:
JVM converts bytecode → Native Machine Code
Stores in memory
Next time it runs directly as machine code
Example:
Bytecode:
iadd
Converted into:
x86 machine instruction:
ADD EAX, EBX
Now CPU executes directly.
FINAL EXECUTION FLOW
Source Code ([Link])
↓
Compiler (javac)
↓
Bytecode ([Link])
↓
JVM Class Loader
↓
Bytecode Verifier
↓
Interpreter / JIT Compiler
↓
Machine Code
↓
CPU Executes
What Happens in Memory During
Execution
Inside JVM:
Each method creates a stack frame containing:
Local Variable Table
Operand Stack
Program Counter
For:
int c = a + b;
Execution:
1. Load a (5)
2. Load b (10)
3. Add → 15
4. Store in c
5. Print result
Output:
15
Feature Bytecode Object Code
Intermediate code for a Virtual Machine-level code for real
Definition
Machine hardware
High-level language compiler (e.g.,
Generated By Native compiler (e.g., gcc)
javac)
Target Specific CPU architecture (x86,
Virtual Machine (JVM, CLR)
Platform ARM)
Portability Platform-independent Platform-dependent
Executed By Process Virtual Machine Directly by CPU
File Example .class, .pyc, .dll (IL) .o, .obj
Needs
No traditional linking required Yes, linked to create executable
Linking?
Speed Slower (interpreted/JIT) Faster (native execution)