01 / 21

IDE Debugging

Master breakpoints, stepping, and variable inspection to find and fix bugs like a professional Java developer.

Java

What Is Debugging?

Debugging is the process of finding and fixing errors (bugs) in your code. Instead of guessing where things go wrong, a debugger lets you pause your running program and examine it from the inside: line by line, variable by variable.

Why Not Just Use Print Statements?

Adding System.out.println() everywhere is a common beginner strategy. It works for simple cases, but it has major drawbacks compared to a real debugger.

Print Statements
Must edit code, recompile, and rerun. Clutters your source. Must remember to remove them later.
IDE Debugger
No code changes needed. Pause anywhere, inspect everything live. Resume or step at will.

The Three Core Concepts

Breakpoints Pause here Stepping Move line by line Inspect See variable values

Setting Breakpoints

A breakpoint is a marker you place on a line of code. When the program reaches that line during execution, it pauses before executing it. You click in the gutter (the left margin) of your editor to toggle a breakpoint.

1
2
3
5
6
public static int sum(int a, int b) { int result = a + b; result = result * 2; // Oops, unintended! return result; // ← breakpoint here }

Stepping Through Code

Once paused at a breakpoint, you can advance execution one line at a time. There are three main stepping commands:

Step Over
Execute the current line and move to the next line in the same method. Does not enter method calls.
Step Into
If the current line calls a method, jump inside that method to see what happens line by line.
Step Out
Finish executing the current method and return to the line that called it.

Step Over vs Step Into

main() int x = 5; int y = doubleIt(x); ← paused print(y); Over doubleIt(int n) int result = n * 2; return result; Step Into Step Out Step Over: skip method Step Into: enter method Step Out: finish + return

Inspecting Variables

While paused, the IDE shows you the current value of every variable in scope. This is the Variables panel (also called Watches). You can also hover over any variable in the editor to see its value.

Variables (Paused at line 4)
a int 3
b int 7
result int 20

The Debug Workflow

1
Reproduce the bug. Run your code and confirm the wrong behavior.
2
Set a breakpoint. Click the gutter at the line where you suspect the problem starts.
3
Run in Debug mode. Use the Debug button (not Run) to start your program.
4
Step and inspect. Advance line by line, checking variable values at each step.
5
Find the mismatch. Identify where the actual value differs from your expectation.
6
Fix, remove breakpoint, test. Correct the code, clear the breakpoint, and verify.

Example: Off-by-One Bug

This method should print numbers 1 through 5, but something is wrong. Let's debug it.

1
3
4
5
public static void printNumbers() { for (int i = 1; i < 5; i++) { System.out.println(i); } }
After loop completes
i int 5
output String "1 2 3 4"

Example: NullPointerException

The program crashes with a NullPointerException. Let's set a breakpoint and find the null variable.

1
2
4
5
String name = getUser(); String greeting = "Hello"; String msg = greeting + " " + name.toUpperCase(); System.out.println(msg);
Variables (Paused at line 3)
name String null
greeting String "Hello"

Example: Logic Error in a Loop

This method should compute the average of an array but gives the wrong answer. Let's trace it.

1
2
3
5
6
public static double average(int[] nums) { int sum = 0; for (int n : nums) { sum += n; } return sum / nums.length; } // average({10, 21, 30}) → expected 20.33, got 20
Variables (Paused at line 4)
sum int 61
nums.length int 3
return value int 20

The Debug Toolbar

Every Java IDE (IntelliJ, Eclipse, VS Code) provides a toolbar with these controls once you are in debug mode:

Resume Step Over Step Into Step Out Stop Evaluate Expression Type any Java expression to test live Evaluate

How to Apply This

1
Identify the symptom. What output did you expect? What did you actually get?
2
Narrow the suspect area. Which method or section of code likely contains the bug?
3
Place breakpoints strategically. Start before the suspicious code, not at every line.
4
Step Over first. Get a high-level view. Only Step Into methods that produce unexpected values.
5
Compare expected vs actual. At each step, ask: "Does this variable hold what I expect?"

Common Mistakes

✕ Too Many Breakpoints
Setting a breakpoint on every line makes stepping tedious. Start with 1-2 strategic breakpoints near the suspected bug.
✕ Using Run Instead of Debug
Clicking the green Run button ignores all breakpoints. You must use the Debug button to activate breakpoints.
✕ Forgetting to Remove Breakpoints
Leftover breakpoints from previous sessions cause unexpected pauses. Clear them when you're done debugging.

Debugging Cheat Sheet

Action What It Does When to Use
Set Breakpoint Marks a line to pause at Before suspicious code
Resume Continues to next breakpoint Done inspecting this area
Step Over Executes current line, moves next Method calls you trust
Step Into Enters the called method Method gives wrong result
Step Out Finishes current method, returns Stepped in too deep
Inspect Variable Shows current value Every step: compare expected vs actual
Evaluate Expression Runs any expression live Test a fix before editing code

Question 1 of 5

What happens when your program reaches a line with a breakpoint during debugging?

Question 2 of 5

You are paused at a line that calls calculateTotal(). You suspect the bug is inside that method. Which stepping command should you use?

Question 3 of 5

You accidentally stepped into a library method you don't need to debug. Which command returns you to your code?

Question 4 of 5

What will the Variables panel show for s when the debugger pauses at Line 2?

Question 5 of 5

What is the main advantage of using an IDE debugger over adding System.out.println() statements?