Java Compile and Execution Flow
This chapter answers the first interview-style questions you collected:
- What is the role of compiler?
- What happens in Java compilation phase?
- What happens in execution phase?

1. Role of Java Compiler (javac)
Java compiler (javac) converts human-readable Java source code (.java) into platform-independent bytecode (.class).
What compiler does
- Syntax checking
- Type checking
- Semantic checking (class/method/variable references)
- Bytecode generation
2. What Happens in Compilation Phase?
When you run:
javac Test.java
compiler performs:
-
Syntax Validation
- Verifies Java grammar, keyword usage, braces, method signatures, class declarations.
- Java is case-sensitive, so identifiers and keywords must match exact case.
-
Type Validation (Strong Typing)
- Ensures assigned values match declared data types.
- Example: assigning text to an
intis rejected at compile time.
-
Comment Ignoring
- Comments are skipped by compiler and do not affect bytecode output.
-
Bytecode Generation
- Produces
.classfile containing JVM bytecode.
- Produces
3. What Happens in Execution Phase?
When you run:
java Test
JVM performs:
- Bytecode Loading via Class Loader.
- Bytecode Verification for security and correctness.
- Memory Allocation for class metadata, objects, stacks, etc.
- Execution by Interpreter + JIT Compiler.
4. End-to-End Example
Test.java --(javac)--> Test.class --(java + JVM)--> Output
javaccreates bytecode.- JVM loads and verifies bytecode.
- Execution engine runs code and prints output.
5. Quick Interview Answer
Q: What is compiler role in Java?
Compiler checks code correctness and converts .java source file into .class bytecode for JVM execution.
Q: What happens in compilation phase?
Syntax check, type check, comment ignoring, bytecode generation.
Q: What happens in execution phase?
Class loading, bytecode verification, memory setup, and execution using interpreter/JIT.