Tutorial 4: Debugging Compilation Errors

Unit 1 · Java setup and first programs

Objectives

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

  1. Reproduce the problem with the smallest input that shows it.
  2. Classify the error: compile-time, runtime, or logic.
  3. Read the first diagnostic and inspect nearby syntax and types.
  4. Make one change, compile, and observe the new evidence.
  5. Add a boundary test so the same defect is less likely to return.

Practice

  1. Repair a missing brace, misspelled method name, and incompatible type assignment.
  2. Debug a program that calculates an average using integer division.
  3. Use temporary output to verify an intermediate value, then remove the diagnostic output.

Self-check

  1. Can a program with a logic error compile successfully?
  2. Why should you start with the first compiler message?
  3. 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?

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

  1. Create three small faulty programs: one compile-time, one runtime, and one logic error.
  2. For each, record the symptom, diagnosis, fix, and regression test.
  3. 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.