Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Section F.6. The clear Command

F.6. The clear Command

In the preceding section, you learned to use the unwatch command to remove a watch on a field. The debugger also provides the clear command to remove a breakpoint from an application. You’ll often need to debug applications containing repetitive actions, such as a loop. You may want to examine the values of variables during several, but possibly not all, of the loop’s iterations. If you set a breakpoint in the body of a loop, the debugger will pause before each execution of the line containing a breakpoint. After determining that the loop is working properly, you may want to remove the breakpoint and allow the remaining iterations to proceed normally. In this section, we use the compound interest application in Fig. 5.6 to demonstrate how the debugger behaves when you set a breakpoint in the body of a for statement and how to remove a breakpoint in the middle of a debugging session.

  1. Opening the Command Prompt window, changing directories and compiling the application for debugging. Open the Command Prompt window, then change to the directory containing the Appendix F examples. For your convenience, we’ve provided a copy of the Interest.java file in this directory. Compile the application for debugging by typing javac -g Interest.java.

  2. Starting the debugger and setting breakpoints. Start the debugger by typing jdb. Set breakpoints at lines 13 and 22 of class Interest by typing stop at Interest:13, then stop at Interest:22 (Fig. F.26).

    Fig. F.26. Setting breakpoints in the Interest application.

  3. Running the application. Run the application by typing run Interest. The application executes until reaching the breakpoint at line 13 (Fig. F.27).

    Fig. F.27. Reaching the breakpoint at line 13 in the Interest application.

  4. Continuing execution. Type cont to continue—the application executes line 13, printing the column headings "Year" and "Amount on deposit". Line 13 appears before the for statement at lines 16–23 in Interest (Fig. 5.6) and thus executes only once. Execution continues past line 13 until the breakpoint at line 22 is reached during the first iteration of the for statement (Fig. F.28).

    Fig. F.28. Reaching the breakpoint at line 22 in the Interest application.

  5. Examining variable values. Type print year to examine the current value of variable year (i.e., the for’s control variable). Print the value of variable amount too (Fig. F.29).

    Fig. F.29. Printing year and amount during the first iteration of Interest’s for.

  6. Continuing execution. Type cont to continue execution. Line 22 executes and prints the current values of year and amount. After the for enters its second iteration, the debugger notifies you that the breakpoint at line 22 has been reached a second time. The debugger pauses each time a line where a breakpoint has been set is about to execute—when the breakpoint appears in a loop, the debugger pauses during each iteration. Print the values of variables year and amount again to see how the values have changed since the first iteration of the for (Fig. F.30).

    Fig. F.30. Printing year and amount during the second iteration of Interest’s for.

  7. Removing a breakpoint. You can display a list of all of the breakpoints in the application by typing clear (Fig. F.31). Suppose you’re satisfied that the Interest application’s for statement is working properly, so you want to remove the breakpoint at line 22 and allow the remaining iterations of the loop to proceed normally. You can remove the breakpoint at line 22 by typing clear Interest:22. Now type clear to list the remaining breakpoints in the application. The debugger should indicate that only the breakpoint at line 13 remains (Fig. F.31). This breakpoint has already been reached and thus will no longer affect execution.

    Fig. F.31. Removing the breakpoint at line 22.

  8. Continuing execution after removing a breakpoint. Type cont to continue execution. Recall that execution last paused before the printf statement in line 22. If the breakpoint at line 22 was removed successfully, continuing the application will produce the correct output for the current and remaining iterations of the for statement without the application halting (Fig. F.32).


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial