Compilation vs.
Runtime Errors
Learn to distinguish between errors caught by the Java compiler before execution and those that crash your program while it runs.
What Are Errors in Java?
Every Java program goes through two major phases before producing output: compilation (translating source code to bytecode) and execution (running the bytecode on the JVM). Errors can occur in either phase, and understanding when an error happens tells you what kind of error it is.
The Two Phases of a Java Program
Why Does This Distinction Matter?
What Causes Compilation Errors?
A compilation error (also called a compile-time error) occurs when your code violates the rules of the Java language. The compiler cannot translate your source code into bytecode.
What Causes Runtime Errors?
A runtime error (exception) occurs when syntactically correct code attempts an impossible operation during execution. The JVM throws an exception and the program terminates unless the error is caught.
nullThe Core Rule
Compilation vs. Runtime: Side by Side
| Compilation Error | Runtime Error | |
|---|---|---|
| When detected | Before execution | During execution |
| Detected by | javac compiler | JVM at runtime |
| Program runs? | No: never starts | Yes: starts, then crashes |
| Common causes | Syntax, type errors | Null refs, bad indices |
| Fixing strategy | Read compiler message | Trace the stack trace |
How to Classify an Error
null? Could an index exceed the array length? Could an integer be divided by zero?Example 1: Missing Semicolon
Example 2: ArrayIndexOutOfBounds
Example 3: Type Mismatch
Example 4: NullPointerException
Tricky Case: Division by Zero
int x = 10 / 0;Throws ArithmeticException at runtime. The compiler does not evaluate expressions to check for zero denominators.
double x = 10.0 / 0;Produces Infinity, not an exception. IEEE 754 defines this behavior. No error occurs at all.
Common Mistakes
arr[n] compiles even if n will be out of bounds at runtime.Question 1 of 5
Consider the following code:
int[] arr = new int[3];arr[3] = 10;
What happens when this code is compiled and run?
Question 2 of 5
Consider the following code:
double d = "hello";
What happens when this line is compiled?
Question 3 of 5
Consider the following code:
String s = null;if (s.equals("test")) { System.out.println("Match");}
What happens when this code is compiled and run?
s is declared as a String and initialized to null, which is valid. The compiler sees s.equals(...) as a legal method call on a String reference. At runtime, the JVM discovers s is null and throws a NullPointerException.Question 4 of 5
Consider the following code:
int x = 5;int y = 0;System.out.println(x / y);
What type of error does this code produce?
y. At runtime, the JVM throws an ArithmeticException: / by zero. Note: if these were doubles (e.g., 5.0 / 0), the result would be Infinity with no exception.Question 5 of 5
Consider the following code:
public static int add(int a, int b) { System.out.println(a + b);}
What happens when this code is compiled?