Testing and Debugging
Debug Trace
A debug flag can print intermediate values while leaving the final result unchanged.
debug flag
A flag lets diagnostic output be turned on or off without changing the calculation.
intermediate value
Printing the intermediate values helps confirm how the result was built.
Debug Trace
DebugTrace.java
Replay: real traced execution (multi-file project)
public class DebugTrace {
public static void main(String[] args) {
boolean debug = true;
int width = 6;
int height = 4;
int area = width * height;
if (debug) {
System.out.println("debug width=" + width + " height=" + height);
}
System.out.println("area=" + area);
}
}
public class DebugTrace {
public static void main(String[] args) {
boolean debug = false;
int width = 6;
int height = 4;
int area = width * height;
if (debug) {
System.out.println("debug width=" + width + " height=" + height);
}
System.out.println("area=" + area);
}
}
debug ← true, width ← 6, height ← 4, area ← 24
1public class DebugTrace {2 public static void main(String[] args) {3 boolean debug→ true = true; //@debug=false, true4 int width→ 6 = 6;5 int height→ 4 = 4;6 int area→ 24 = width6 * height4;if (debug)
8if (debugtrue) {9 System.out.println("debug width=" + width6 + " height=" + height4);10}outputdebug width=6 height=4System.out.println("area=" + area);
12 System.out.println("area=" + area24);13}outputarea=24
debug ← false, width ← 6, height ← 4, area ← 24
1public class DebugTrace {2 public static void main(String[] args) {3 boolean debug→ false = false;4 int width→ 6 = 6;5 int height→ 4 = 4;6 int area→ 24 = width6 * height4;78 if (debug) {9 System.out.println("debug width=" + width + " height=" + height);10 }1112 System.out.println("area=" + area24);13 }outputarea=24