Level 2 – Debugger Basics (≈ 12 minutes)
Concepts: breakpoints, stepping over/into/out, variable inspection, tracking changing state, logic bugs.
We reuse the Level 1 project. You will debug with breakpoints and stepping, not only console output.
1. Bring the bugs back (without losing your fixes)
You should still have:
CalculatorExercises.javaLoopExercises.javaMain.java
If you have not already (see Level 1, section 8): comment out your Level 1 fixes in divide, sumFirstThree, and countUpTo so the original buggy lines run again. Keep the commented fix code in the file so you can uncomment it later instead of retyping.
Arrays in OnlineGDB: you usually cannot inspect array elements or lengths in the Variables panel. The starter sumFirstThree includes a local numberCount so you can see how many elements the array has while debugging.
2. Set your first breakpoint
- Open
Main.java. - Click in the gutter on the line:
int result = CalculatorExercises.divide(10, 0);
- You should see a red dot (breakpoint) next to that line.
Start the debugger:
- Click Debug.
- Click the green Start button.
- When execution pauses, the debugger is stopped at your breakpoint.
3. Step over, then Continue
When paused at the breakpoint:
- Look at the Variables/Locals panel for
argsand any locals. - Click Step Over once on the
divideline. - Click Continue so the program runs forward. In OnlineGDB, stepping alone may not surface the failure you expect; Continue lets execution run until the exception or the next breakpoint.
If an exception appears:
- Note the line where execution stops.
- Compare to the stack trace you saw in Level 1.
Repeat with a breakpoint on return a / b; inside CalculatorExercises.divide:
- Click Debug, then the green Start button.
- Inspect
aandbin the Variables panel right before the division.
4. Fix divide before the next exercise
Before you go on to section 5: restore your divide fix so dividing by zero no longer crashes (uncomment your Level 1 fix or re-apply it). Run with Run once to confirm the program reaches the sumFirstThree section.
If divide still throws, you will not reach the next breakpoint in main.
5. Step into sumFirstThree
Practice Step Into and Step Out with sumFirstThree:
- Set a breakpoint in
Main.mainon:int sum = CalculatorExercises.sumFirstThree(nums);
- Click Debug, then the green Start button.
- When paused on this line, click Step Into.
You should now be inside CalculatorExercises.sumFirstThree.
While stepping through the loop:
- Watch
numberCount,i, andtotalin the Variables panel (notnumbers.lengthor array slots). - Use Step Over to run each iteration.
- Notice when
iis no longer a valid index.
In OnlineGDB, skip watch expressions and rely on Variables/Locals.
When you understand the failure:
- Click Stop to end the debugging session.
- Uncomment your Level 1 fix for
sumFirstThree(or re-apply it) so short arrays do not crash and the first three elements are summed when present.
6. Logic bug: countUpTo
- Set a breakpoint in
Mainon:int count = LoopExercises.countUpTo(5);
- Click Debug, then the green Start button.
- When paused, click Step Into to enter
countUpTo. - Use Step Over through the loop.
Observe:
- How many times does the loop run?
- What are the values of
iandcountat each step?
Then uncomment your Level 1 fix for countUpTo (or re-apply it) so the result matches what you expect.
Re-run with the debugger to confirm.
7. After the debugger exercises
Uncomment any remaining commented Level 1 fixes so divide, sumFirstThree, and countUpTo are all corrected and the program runs cleanly. You should not need to redo Level 1 from scratch.
8. Compare debugger vs print debugging
Briefly discuss or think about:
- When was the debugger faster or clearer than
System.out.println? - When might print debugging still be useful?
- How did stepping into
sumFirstThreeandcountUpTohelp you see the bug more clearly?
You are now ready for Level 3 (a new OnlineGDB project with a different Main.java).