Java Debugging
Find a wrong result by printing the values you already have, then fix the bad line. In Yard.java, set int x = 10; and int y = 2;. Print x and y. Compute int sum = x - y; by mistake and print sum. Compile with javac Yard.java and run java Yard. The screen should show the two values and then 8. A second demo changes the operator to + so sum prints 12. Leave Javadoc for the next page — this lesson is reading the screen to catch a logic mistake.
Print the values around a wrong sum
The equals page compared string text. This page hunts a bad arithmetic line. Open Yard.java. Keep the public class name Yard. Delete leftover equals / == demos if they bury the debug story, leftover Machine horn files, leftover path demos, and any keyboard Scanner. The program must not wait for typing. No import line is required.
Inside Yard.main, set the numbers and print them before you trust the sum:
int x = 10;
int y = 2;
System.out.println("x=" + x);
System.out.println("y=" + y);
Then keep a buggy line on purpose for the first demo:
int sum = x - y;
System.out.println(sum);
The prints of x and y prove the inputs. The printed 8 proves the operator is wrong for a sum.
public class Yard {
public static void main(String[] args) {
int x = 10;
int y = 2;
System.out.println("x=" + x);
System.out.println("y=" + y);
int sum = x - y;
System.out.println(sum);
}
}
From the folder that holds Yard.java, run javac Yard.java. When parentheses, braces, and semicolons match, javac writes no line of its own. Then run java Yard — not java Yard.java. Leave the .java ending off the run command. Keep one public class in the file so the name matches Yard.
The screen shows x=10, then y=2, then 8. Walk the run once. Java starts at Yard.main. The first two prints confirm the stored numbers. The third print shows the result of subtraction, not addition. The program compiled and ran — this is a logic mistake, not a missing semicolon.
Do the math on paper once beside the screen: ten minus two is eight. That matches the last line, so the bug is not a wrong variable value; the bug is the operator you chose for a sum. When the printed inputs look right and the result looks wrong, search the expression that built the result.
If you still see true / false from an equals file, switch to the debugging teaching file and run java Yard again. If you change the operator without printing x and y, you guess in the dark. Keep the value prints until the screen matches what you expect for the inputs. Leave temporary labels in place while you hunt; you can tidy them after the fix is proven.
Say the rule out loud while you watch the screen: print the facts, read the wrong result, then change the line that caused it. Do not invent a Javadoc comment block as the fix on this page. Javadoc waits for the next lesson.
Fix the operator and run again
A second program keeps the same prints of x and y. The sum line uses + instead of -. The last print should show 12.
public class Yard {
public static void main(String[] args) {
int x = 10;
int y = 2;
System.out.println("x=" + x);
System.out.println("y=" + y);
int sum = x + y;
System.out.println(sum);
}
}
Compile with javac Yard.java, then run java Yard. The screen shows x=10, then y=2, then 12. The inputs did not change. The operator did. That is the debug loop for this beginner shape: see the values, spot the bad operation, fix it, compile again, run again.
Read the sum line once more: int sum = x + y;. Keep the label prints so a later mistake still shows the inputs. When the last number matches 10 + 2, the logic line is fixed for this demo. Compare the two teaching files side by side if both sit open: the only intentional difference should be - versus + on the sum line.
If leftover equals demos still sit nearby, keep them as history. For this lesson run the debugging teaching file so the screen shows numbers, not string booleans. If javac complains about duplicate public classes, keep only Yard public.
The table is the short map of this page. First you look. Then you print. Then you change one clear line and verify. Skip guessing without a screen. Skip deleting all prints before you know the fix worked.
📊 Beginner debug steps on Yard
| Step | What you do |
|---|---|
| 1 | Run java Yard and read every printed line |
| 2 | Add println for the values you distrust |
| 3 | Fix the bad operator or condition, then compile and run again |
Compared with equals checks
On the equals page, equals and == answered content versus identity for strings. On this page, println answers what your numbers really are when a result looks wrong. You still compile Yard.java and run java Yard. The new habits are labeled value prints, reading a wrong result as a clue, and re-running after a one-line fix.
Debugging here is not a special menu. It is ordinary output used as a flashlight on your own variables. When the flashlight shows good inputs and a bad result, the expression that built the result is the suspect.
If a leftover a.equals(b) file is still open, you will see true and false. Switch to the debugging file so main prints x, y, and sum. Keep Javadoc demos off this page. Keep keyboard input off this page.
Before Javadoc
You can print x and y, keep a minus bug, and see 8 after the labels. You can change the operator to plus and see 12. Compile Yard.java and run java Yard for each kept demo in turn. The screen told you where the logic failed. The next page is Javadoc, where you document methods with special comments for readers and tools.
Keep the buggy demo for a clear wrong screen. Keep the fixed demo for a clear verify step. Keep Javadoc files off this page. Those three habits are the checkpoint before you move on. When labeled prints plus a corrected sum match 10 + 2, debugging did its job for this lesson and you are ready for the next topic about Javadoc.
One last pass through the folder: one public Yard, value prints around the sum, no keyboard wait, no forced documentation comments. Run javac Yard.java and java Yard once more on the demo you keep open. If the screen still shows the inputs and the result you expect, the debug loop is solid and the course can move to Javadoc on the next page. Prefer one clear fix at a time so each new run teaches one change.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top