Overview of Debugging Tools

In this lesson, we discuss the general features of the debugging tools available in typical integrated development environments, or IDEs.

Using println statements to trace execution

We have learned of our need to examine the intermediate results of Java statements at specific places within a program and at certain points in time. By doing so, we would likely uncover the source of a mistake in the program’s logic. Up to now, we have exhibited these results simply by inserting println statements in our code. Although this technique uses a familiar statement, we ultimately are left with a program containing extraneous statements. If we remove these statements, we might find later that we need to insert them again to find more bugs. Embedding such println statements within an if statement that tests the value of a Boolean constant DEBUG, as we did in the previous debugging interlude, is one way to retain debugging statements but ignore them after debugging is finished. However, any time we change the value of DEBUG, we must compile the program again.

Although inserting println statements at key points in a program is useful, particularly for short programs—and should remain among our strategies for debugging—both Java and IDEs provide other ways that offer the debugging benefits of println statements without the downside mentioned previously.

Debugging tools in an IDE

Typically, integrated development environments, or IDEs, provide powerful debugging tools. Although details of accessing these tools vary from one IDE to another, they all share some basic features.

Breakpoints and watches

For example, we can stop a program’s execution and examine the values of key variables by setting a breakpoint at various places within the program. When program execution reaches a breakpoint, it stops until we indicate that it should resume. During the pause, we can look at the values of certain variables to see whether the computation is progressing normally. To do so, we just set a watch for each variable of interest before we run the program.

Single-step

In addition to using breakpoints and watches, we can single-step through all or part of a program. That is, we can execute one statement at a time, examining the results along the way. If a method is called, we can either step into the method to trace its statements or step over the method to skip those details but continue with the method’s result. We also can let a program run normally—and quickly—until it reaches a breakpoint, single-step through a portion that needs close examination, and finally resume normal execution.

Get hands-on with 1200+ tech skills courses.