Read a compiler diagnostic as a location and explanation.
Use a small, repeatable debugging process.
Three kinds of failure
A compile-time error prevents bytecode from being produced. A runtime error occurs while a compiled program runs, such as dividing by zero. A logic error produces a result, but not the intended result. Each category calls for a different investigation.
public class DebugDemo {
public static void main(String[] args) {
int total = 10
System.out.println(total);
}
}
The missing semicolon is a compile-time error. Read the first diagnostic carefully, inspect the reported line and the line above it, then make one small correction. Do not blindly fix every later error before recompiling; one missing delimiter can cause a cascade.
A practical workflow
Reproduce the problem with the smallest input that shows it.
Classify the error: compile-time, runtime, or logic.
Read the first diagnostic and inspect nearby syntax and types.
Make one change, compile, and observe the new evidence.
Add a boundary test so the same defect is less likely to return.
Practice
Repair a missing brace, misspelled method name, and incompatible type assignment.
Debug a program that calculates an average using integer division.
Use temporary output to verify an intermediate value, then remove the diagnostic output.
Self-check
Can a program with a logic error compile successfully?
Why should you start with the first compiler message?
What test exposes integer-division mistakes?
Mastery task: Create a deliberately faulty CourseCard program, record each diagnostic, fix one issue at a time, and write a short debugging log explaining your reasoning.
Self-Check Quiz
1. Which error prevents compilation?
(A) Logic error
(B) Runtime error
(C) Compile-time error
(D) Input error
Answer(C) A compile-time error prevents the compiler from producing usable bytecode.
2. What should you inspect first when several diagnostics appear?
AnswerInspect the first diagnostic and its nearby source lines; one missing symbol or delimiter can cause cascading messages.
3. Can a program with a logic error compile?
AnswerYes. Logic errors are incorrect decisions or calculations in otherwise valid Java code.
Exercises
Exercise 1: Classify the Failure
Classify each: missing semicolon, dividing by zero, and calculating an average with integer division.
Sample answerMissing semicolon is compile-time; dividing by zero is usually runtime for integer arithmetic; integer division in an average is a logic error.
Exercise 2: Debug the Program
public class Total {
public static void main(String[] args) {
int first = 8;
int second = 4
System.out.println(first + second);
}
}
Sample answerAdd the missing semicolon after int second = 4;. Then compile again and test the expected output, 12.
Homework
Create three small faulty programs: one compile-time, one runtime, and one logic error.
For each, record the symptom, diagnosis, fix, and regression test.
Write a debugging checklist you can use for every future Java assignment.
Sample homework answer
A useful checklist is: reproduce the failure; classify it; read the first diagnostic; inspect nearby lines and types; make one change; recompile; run a boundary test; remove temporary debug output. The regression test should fail before the fix and pass afterward.