Using the Debugger
Your program compiles and runs, but the answer is wrong. You could sprinkle println lines everywhere and guess. Or you could pause the program at any line, look at every variable, and move forward one step at a time until you see exactly where it goes wrong. That's what a debugger does, and learning it early is one of the best habits you can build.
A Program with a Bug
This program should print the average of three test marks: 78, 92 and 86. They add up to 256, so the average should be about 85.33. Look at what it prints instead.
public class Average {
public static void main(String[] args) {
int[] marks = {78, 92, 86};
int total = 0;
for (int mark : marks) {
total = total + mark;
}
double average = total / marks.length;
System.out.println("Total: " + total);
System.out.println("Average: " + average);
}
}Total: 256
Average: 85.0The total is right, 256, but 256 / 3 should be 85.33, not 85.0. Where did the decimal part go? Let's find out with the debugger instead of guessing.
Set a Breakpoint and Start Debugging
A breakpoint is a marker on a line that says "pause here". The program runs normally until it reaches that line, then stops before running it.
- Open
Average.javain your IDE. - Click in the grey margin to the left of line 6. A red dot appears: that's your breakpoint.
- Start the program with Debug instead of Run (the bug icon, or Shift + F9 in IntelliJ IDEA on Windows and Linux).
The program pauses on line 6, and the Variables panel shows the current values: marks, total and mark.
Step Through the Code
These buttons control what happens next:
| Button | IntelliJ IDEA (Windows / Linux) | What it does |
|---|---|---|
| Step Over | F8 | Runs the current line and pauses on the next one |
| Step Into | F7 | If the line calls one of your methods, goes inside it |
| Step Out | Shift + F8 | Finishes the current method and pauses where it was called |
| Resume | F9 | Runs until the next breakpoint (or the end) |
| Stop | Ctrl + F2 | Ends the program |
Press Resume and the program runs round the loop once and pauses on line 6 again. Watch total in the Variables panel: 0, then 78, then 170. The loop is adding correctly. Now remove the breakpoint and Step Over until line 8 has run: total is 256, but average shows 85.0.
Use a Watch to Test an Idea
A watch is an expression the debugger works out for you at every pause. In the Variables panel, add these two watches:
total / marks.lengthshows 85(double) total / marks.lengthshows 85.33333333333333
That's the bug. total and marks.length are both int, so Java does whole-number division first and gets 85. Only then is 85 stored in the double, as 85.0. The decimal part was already gone.
Here's the fixed line 8. Converting total to double first makes the division keep its decimal part:
public class AverageFixed {
public static void main(String[] args) {
int[] marks = {78, 92, 86};
int total = 0;
for (int mark : marks) {
total = total + mark;
}
double average = (double) total / marks.length;
System.out.println("Total: " + total);
System.out.println("Average: " + average);
}
}Total: 256
Average: 85.33333333333333Try it yourself: set a breakpoint on line 8 of AverageFixed and add a watch for average after stepping over it. Does it show the decimal part now?
Common Mistakes
Pressing Run instead of Debug. Breakpoints are ignored in a normal run. Use the Debug button.
Stepping into library code. Step Into on System.out.println can take you deep into Java's own classes. Use Step Out to come back, and Step Over for lines that only call library methods.
Changing code while paused and expecting it to apply. Most edits only take effect after you stop and debug again.
Leaving debugging println lines in the code. Once you've found the bug with the debugger, there's nothing to clean up. That's another reason to prefer it over println.
Practice
- Easy: Debug
Averagewith a breakpoint on line 6 and write down the value oftotalat each pause. - Harder: Change the marks to
{90, 95, 100, 99}and use a conditional breakpoint so the debugger only pauses whenmark == 100. What istotalat that moment?
Key Takeaways
Was this tutorial helpful?