Week 15 · 15.2 · Tue, Nov 10 · The Specific Skills + Try Its
Trace It: Real Java, One Line at a Time
This section starts with one look back, at a true-or-false box that keeps its answer. Then it shows the five kinds of steps on today's bench: reading an assignment, building a trace table, keeping a box's last value, tracing boxes of different types, and finding the row where a trace first goes wrong.
A True-or-False Box Keeps Its Answer
Big idea. A boolean box stores the answer to its question at the moment its line runs. When a box it asked about changes later, the answer stays the same until a line asks the question again.
A boolean box holds true or false. The line that stores the answer works out the question once, with the current values. A later line that changes another box does not change the answer. Only a line that asks again stores a new answer.
Worked example. Trace every box and predict exactly what is printed.
1int age = 70; 2boolean senior = age > 64; 3System.out.println(senior); 4age = 64; 5System.out.println(senior); 6senior = age > 64; 7System.out.println(senior);
line age senior Output 1 70 2 70 true 3 70 true true 4 64 true 5 64 true true 6 64 false 7 64 false false
true true false
Many readers change senior to false on line 4. But line 4 changes only age, so senior still holds true, and line 5 prints true. Line 6 asks again with age's current value, 64. 64 is not greater than 64, so senior now holds false, and line 7 prints false.
Reading an Assignment
An assignment stores a value in a box. Java reads it in a fixed order.
How to read an assignment:
- Look at the right side of =.
- Work it out with the current value of every box it names.
- Store the result in the box on the left. The old value in that box is gone.
Example 1. One box changes twice.
1int n = 6; 2n = n * 2; 3n = n - 4; 4System.out.println(n);
Show the solution
8
Line 2 works out 6 * 2 with the current value of n, so n holds 12. Line 3 works out 12 - 4, so n holds 8. Line 4 prints 8.
Building a Trace Table
A trace table has a column for the line number, one column for each box and an Output column if anything prints. Each row shows every box right after that line runs. A box that has not been made yet is left empty.
Example 2. Trace two boxes.
1int a = 5; 2int b = a + 1; 3a = b * 2; 4System.out.println(a);
Show the solution
line a b Output 1 5 2 5 6 3 12 6 4 12 6 12
Row 3 uses b's current value, 6, so a becomes 12. On line 3, b does not change, so its 6 is copied forward.
The Last Value Wins
A box keeps its last value until a line stores a new one. A box that was copied from another box does not follow that box later.
Example 3. A copy keeps its value.
1int score = 7; 2int copy = score; 3score = 20; 4System.out.println(copy);
Show the solution
7
Line 2 copies 7 into copy. Line 3 changes only score, so copy still holds 7.
Boxes of Different Types
A trace table works the same way for every type. A double holds a decimal, a boolean holds true or false and a String holds text. When one side of + is text, + joins.
Example 4. A decimal and a true-or-false box.
1double cost = 1.5; 2cost = cost * 3; 3boolean big = cost > 4; 4System.out.println(cost); 5System.out.println(big);
Show the solution
4.5 true
Line 2 makes cost 4.5. Line 3 asks whether 4.5 is greater than 4 and stores the answer, true, in big.
Example 5. Text that grows.
1String team = "Saints"; 2team = team + " win"; 3System.out.println(team);
Show the solution
Saints win
The space is inside the quotation marks, so it is part of the text that is joined on.
Finding the Wrong Row
A trace table also shows where a mistake starts. A wrong row makes every later row that uses it wrong too, so the first wrong row is the one to fix.
Example 6. Find the first wrong row.
1int x = 2; 2int y = x + 3; 3x = y * 2; 4y = y - x;
Show the solution
Here is a student's table.
line x y 1 2 2 2 5 3 10 5 4 10 3
Rows 1 to 3 are right. Row 4 is wrong: line 4 must use the current value of x, which is 10, so y is 5 - 10, which is -5. The student used x's old value, 2.
The bench catches three common mistakes, and each one changes the answer. The first is using a box's old value after a line has changed it. The second is changing a copy when the box it came from changes. The third is forgetting to copy forward a box that a line did not change.
Sources: Oracle, The Java Tutorials: Variables, Primitive Data Types and Assignment, Arithmetic, and Unary Operators; Oracle, The Java Language Specification, Java SE 21 Edition, ยง15.26.1; course repository, Bench Basics B16 job data (2026), run with the course JDK.
Work each one in your notebook first, then check it.
Try It 1. What is printed? The first line printed is first.
1int speed = 45; 2boolean fast = speed > 40; 3speed = 30; 4System.out.println(fast); 5fast = speed > 40; 6System.out.println(fast);
Try It 2. The lines int k = 3; then k = k + 4; then k = k * 2; run in order. What does k hold at the end?
Try It 3. What does Java do first when the line total = total + 5; runs?
Try It 4. How many rows, not counting the header, does a trace table need for int a = 1; then int b = 2; then a = a + b; then System.out.println(a); ?
Try It 5. The lines int p = 4; then int q = p + 1; then p = q * 3; run in order. What does row 3 of the trace table show?
Try It 6. The lines int m = 9; then int n = m; then m = 2; then System.out.println(n); run in order. What is printed?
Try It 7. The lines int t = 1; then t = 5; then t = 8; then System.out.println(t); run in order. What is printed?
Try It 8. The lines double w = 1.5; then w = w + 0.25; run in order. What does w hold?
Try It 9. The lines String s = "Who"; then s = s + " Dat"; then System.out.println(s); run in order. What is printed?
Try It 10. The lines int c = 3; then int d = c * 2; then c = c + d; then d = d - c; run in order. A student's rows read c 3; then c 3 and d 6; then c 9 and d 6; then c 9 and d 3. Which row is wrong first?
Try It 11. Why is the first wrong row the one to fix first?