Compile Time vs Runtime In Java
πΉ Compile-Time
Occurs before the program runs, during the code compilation process.
β Characteristics:
- Syntax errors detected (e.g., missing semicolon, wrong variable type).
- Code is converted from
.java
to.class
files (bytecode). - Checked by the Java compiler (
javac
).
π§ Examples:
- Misspelled variable name
- Type mismatch:
int x = "abc";
- Calling a method that doesnβt exist
πΈ Run-Time
Occurs while the program is running, after successful compilation.
β Characteristics:
- Logic errors, memory issues, exceptions can occur.
- Managed by the JVM (Java Virtual Machine).
π§ Examples:
NullPointerException
- Division by zero
- File not found
π Summary Table:
Feature | Compile-Time | Run-Time |
---|---|---|
When it occurs | Before execution | During execution |
Checked by | Compiler (javac ) |
JVM (Java Virtual Machine) |
Catches | Syntax & type errors | Exceptions & logic errors |
Example error |
Missing ; , Type mismatch
|
NullPointerException , ArithmeticException
|