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
int x = "hello"; // ❌ Compile-time error

πŸ”Έ 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
String str = null;
System.out.println(str.length()); // ❌ Run-time error (NullPointerException)

πŸ“Š 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
Back to blog

Leave a comment

Please note, comments need to be approved before they are published.