Stop 11 · Git, Build Tools & TestingTopics · 10/10

Debugging in IntelliJ

Part of the Java Roadmap

In short

Sprinkling println everywhere and re-running is slow guesswork. IntelliJ's debugger freezes your program at any line and lets you look inside every variable, so you see the bug instead of guessing.

IntelliJ's debugger lets you pause on breakpoints, step through code, inspect variables and evaluate expressions while the program runs, which is far faster than print debugging.

The jaadu squints through a big magnifying glass at a single frozen frame of a film strip held up to the light.

Think of it like this

Pausing the cricket replay

When the umpire isn't sure about a run-out, the third umpire doesn't watch the whole match again. He pauses at the exact frame, moves forward one frame at a time and zooms in on the bat and crease. A breakpoint is the pause, stepping is frame-by-frame, and the variables panel is the zoom.

The big ideas

What you need to know

Breakpoints

Pause before the line runs. Right-click a breakpoint to add a condition like i == 999, so it only stops when it matters.

Step over vs into

Step Over treats a method call as one step. Step Into goes inside it. Step Out (Shift+F8) finishes the current method.

Watches and evaluate

Add a watch to track an expression, or evaluate any expression like total / (double) marks.length while paused.

The Frames panel

Shows the call stack: which method called which. Click a frame to see that method's variables.

How it works

A debugging session

  1. 1

    Set a breakpoint

    Click in the gutter next to a line number. A red dot appears.

  2. 2

    Start in debug mode

    Click the bug icon or press Shift+F9 (Windows and Linux). The program stops at the red dot.

  3. 3

    Step

    Step Over (F8) runs the line, Step Into (F7) enters a method call, Resume (F9) runs to the next breakpoint.

  4. 4

    Inspect

    Read variables in the Variables panel, or use Evaluate Expression (Alt+F8) to try code on the paused state.

  5. 5

    Fix and re-run

    Once you see the wrong value, fix the code and run the test or program again.

See it in code

Walk through the code

java
1public class MarksReport {2    static double average(int[] marks) {3        int total = 0;4        for (int i = 0; i < marks.length; i++) {5            total += marks[i];6        }7        return total / marks.length;   // set a breakpoint here8    }9 10    public static void main(String[] args) {11        int[] marks = {78, 85, 91};12        System.out.println(average(marks)); // expected 84.67, prints 84.013    }14}

1/3Put a breakpoint here and start debugging. The Variables panel shows total = 254 and marks.length = 3.

A classic bug to practise on. The fix is return (double) total / marks.length;

Save yourself the pain

Common mistakes

Oops

Adding a breakpoint inside a loop that runs 10,000 times and pressing Resume over and over.

Do this instead

Right-click the breakpoint and add a condition, like i == 9999 or total < 0.

Oops

Running with the green Run button and wondering why breakpoints are ignored.

Do this instead

Breakpoints only work in Debug mode. Use the bug icon.

Oops

Stepping into JDK library code by accident and getting lost.

Do this instead

Press Step Out (Shift+F8) to return, and use Step Over for calls you trust.

Your mission

Bug hunt

~30 min

Quick check

You are paused on a line that calls calculateTax(order). You want to see what happens inside calculateTax. Which action?

Go deeper

  1. 1Set a breakpoint and use Step Over, Step Into and Resume.
  2. 2Add a conditional breakpoint that only stops for one value.
  3. 3Use Evaluate Expression to test a fix without restarting.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge