01 / 20

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.

AP Computer Science A

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

Source Code .java file javac Compiler Checks syntax & types Compilation Error Bytecode .class file JVM Execution java MyProgram Runtime Error PHASE 1 Write PHASE 2 Compile PHASE 3 Run

Why Does This Distinction Matter?

✓ Compilation Errors
Caught before the program runs. Your code never executes. The compiler tells you the exact line and what went wrong.
✕ Runtime Errors
Program compiles fine but crashes during execution. May only appear with certain inputs, making them harder to find.

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.

1
Syntax errors: missing semicolons, unmatched braces, misspelled keywords
2
Type mismatches: assigning a String to an int variable, incompatible return types
3
Undeclared variables: using a variable name that was never declared in scope
4
Access violations: calling a private method from outside its class

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.

1
NullPointerException: calling a method on a reference that is null
2
ArrayIndexOutOfBoundsException: accessing an index beyond the array's length
3
ArithmeticException: dividing an integer by zero
4
ClassCastException: casting an object to an incompatible type at runtime

The Core Rule

Can the compiler detect it? YES Compilation Error Program never runs EXAMPLES • Missing semicolons • Type mismatches • Undeclared variables NO Runtime Error Crashes during execution EXAMPLES • Null dereference • Index out of bounds • Division by zero (int)

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

1
Read the code carefully. Look for syntax issues: missing semicolons, unclosed braces, misspelled keywords.
2
Check types. Is every variable used with the correct type? Are method return types compatible with assignments?
3
Check declarations. Is every variable declared in scope before use? Are all methods that are called actually defined?
4
If no compiler rule is broken: trace through the logic. Could any value be null? Could an index exceed the array length? Could an integer be divided by zero?
5
Classify: if the compiler would reject it, it is a compilation error. If it compiles but fails during execution, it is a runtime error.

Example 1: Missing Semicolon

JAVA CODE
public class Main { public static void main(String[] args) { int x = 10 System.out.println(x); } }
Main.java:3: error: ';' expected int x = 10 ^

Example 2: ArrayIndexOutOfBounds

JAVA CODE
public class Main { public static void main(String[] args) { int[] nums = {1, 2, 3}; System.out.println(nums[5]); } }
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3

Example 3: Type Mismatch

JAVA CODE
public class Main { public static void main(String[] args) { int score = "ninety"; System.out.println(score); } }
Main.java:3: error: incompatible types: String cannot be converted to int

Example 4: NullPointerException

JAVA CODE
public class Main { public static void main(String[] args) { String name = null; System.out.println(name.length()); } }
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null

Tricky Case: Division by Zero

Integer Division
int x = 10 / 0;

Throws ArithmeticException at runtime. The compiler does not evaluate expressions to check for zero denominators.
Double Division
double x = 10.0 / 0;

Produces Infinity, not an exception. IEEE 754 defines this behavior. No error occurs at all.

Common Mistakes

✕ Confusing Logic Errors
A logic error produces wrong output but does not crash. It is neither a compilation error nor a runtime error: the program runs to completion with incorrect results.
✕ Assuming the Compiler Checks Values
The compiler checks types and syntax, not the actual values variables hold. An index like arr[n] compiles even if n will be out of bounds at runtime.
✕ Thinking All Exceptions are Runtime Errors
Java has checked exceptions (like IOException) that must be caught or declared at compile time. Not handling them causes a compilation error, not a runtime error.

Question 1 of 5

What happens when this code is compiled and run?

Question 2 of 5

What happens when this line is compiled?

Question 3 of 5

What happens when this code is compiled and run?

Question 4 of 5

What type of error does this code produce?

Question 5 of 5

What happens when this code is compiled?