Revision & Debugging 复习与程序调试
Learning Objectives 学习目标
- Classify program errors into Syntax, Runtime, and Logical categories.
- Trace variable states using dry runs to isolate code bugs.
- Inspect debug trace print routines to troubleshoot logic flaws.
- 将程序错误分类为语法、运行时和逻辑错误。
- 通过手动追踪 (dry run) 变量状态来隔离代码缺陷。
- 检查调试追踪打印程序以排查逻辑漏洞。
2 · Program Error Classification
Errors in programming are generally grouped into three main categories. Understanding when and how they are detected is key to fixing them: 编程中的错误通常分为三个主要类别。理解何时以及如何检测到它们是修复它们的关键:
| Error Category错误类别 | Detection Time检测时间点 | Description & Example说明与示例 | Crash Program?导致程序崩溃? |
|---|---|---|---|
| Syntax Error / 语法错误 | Compile Time | Violates Java language rules. Checked by compiler. Example: Missing semicolons, unmatched braces.违反 Java 语言语法规则。由编译器检查。 示例:遗漏分号,大括号不匹配。 |
Yes (Blocks execution) |
| Runtime Error / 运行时错误 | Execution Time | Occurs during execution, causing JVM to abort. Example: Division by zero, accessing out-of-bounds array.在执行期间发生,导致 JVM 中断。 示例:除以零,访问超出边界的数组。 |
Yes (Abrupt termination) |
| Logical Error / 逻辑错误 | Post-Execution | Program runs but yields incorrect outputs due to bad formulas. Example: Using + instead of *, incorrect operator precedence.程序运行成功但由于错误的算术公式导致输出错误。示例:误用 + 代替 *,运算符优先级错误。 |
No (Runs to completion) |
3 · Code Tracing (Dry Runs)
A dry run is a manual mental compilation process. You trace each line of code step-by-step on paper, creating a trace table to record variable value changes. This is the most reliable way to find logical errors without debugging tools. 手动追踪是一种手工模拟编译的过程。您在纸上逐步追踪每行代码,创建一个追踪表来记录变量值的变化。这是在没有调试工具的情况下寻找逻辑错误最可靠的方法。
4 · Trace Debugging Process
If a program outputs unexpected results, insert temporary trace statements (System.out.println()) to log variable states at critical checkpoints. This isolates logic flaws:
如果程序输出了异常结果,可在关键检查点插入临时的追踪打印语句(System.out.println())记录变量状态。这可以有效隔离逻辑漏洞:
Applied Case Study: Trace Sandbox应用案例研究:代码调试沙盒
Company: Kompas Debugging Sandbox.
Let's examine a program containing a logical error in the arithmetic average calculation. Inspect the bug and the solution.
公司: Kompas Debugging Sandbox。
让我们检查一个在计算算术平均值时包含逻辑错误的程序。排查漏洞及修复方案。
Buggy Averages Calculator有逻辑缺陷的平均值计算程序
public class BuggyAverage {{ public static void main(String[] args) {{ int a = 10; int b = 20; int c = 30; // Logical Error: Operator precedence division runs first! // Evaluates as: 10 + 20 + (30 / 3) = 10 + 20 + 10 = 40.0 double avg = a + b + c / 3.0; System.out.println("Computed average: " + avg); }} }}
Corrected Averages Calculator修复后的平均值计算程序
public class FixedAverage {{ public static void main(String[] args) {{ int a = 10; int b = 20; int c = 30; // Fixed: Parentheses enforce addition sum runs before division // Evaluates as: (10 + 20 + 30) / 3.0 = 60 / 3.0 = 20.0 double avg = (a + b + c) / 3.0; System.out.println("Correct average: " + avg); }} }}
Check Your Understanding自我检测
Q1. What type of error is a missing semicolon?遗漏分号属于什么类型的错误?
Q2. A program runs and compiles successfully, but displays incorrect total sales. What type of error occurred?程序成功编译并运行,但显示了错误的销售总额。发生了什么类型的错误?
Q3. Evaluate the output of buggy expression: double val = 4 + 6 / 2.0; What is value of val?评估有缺陷的表达式的输出结果:double val = 4 + 6 / 2.0; val 的值是多少?