Java Reference
In-Depth Information
realize that a debugger, while more convenient than print commands, is not cost-free.
It does take time to set up and carry out an effective debugging session.
In actual practice, you cannot avoid using a debugger. The larger your programs get,
the harder it is to debug them simply by inserting print commands. You will find that
the time investment to learn about a debugger is amply repaid in your programming
career.
Like compilers, debuggers vary widely from one system to another. On some systems
they are quite primitive and require you to memorize a small set of arcane commands;
on others they have an intuitive window interface. The screen shots in this chapter
show the debugger in the Eclipse development environment, downloadable for free
from the Eclipse Foundation web site [ 4 ]. Other integrated environments, such as
BlueJ, also include debuggers. A free standalone debugger called JSwat is available
from the JSwat Graphical Java Debugger web page [ 5 ].
You will have to find out how to prepare a program for debugging and how to start a
debugger on your system. If you use an integrated development environment, which
contains an editor, compiler, and debugger, this step is usually very easy. You just
build the program in the usual way and pick a menu command to start debugging. On
some systems, you must manually build a debug version of your program and invoke
the debugger.
Once you have started the debugger, you can go a long way with just three debugging
commands: Ȓset breakpointȓ, Ȓsingle stepȓ, and Ȓinspect variableȓ. The names and
keystrokes or mouse clicks for these commands differ widely between debuggers, but
all debuggers support these basic commands. You can find out how, either from the
documentation or a lab manual, or by asking someone who has used the debugger
before.
You can make effective use of a debugger by mastering just three concepts:
breakpoints, single-stepping, and inspecting variables.
264
265
When you start the debugger, it runs at full speed until it reaches a breakpoint. Then
execution stops, and the breakpoint that causes the stop is displayed (see Figure 5 ).
You can now inspect variables and step through the program a line at a time, or
Search WWH ::




Custom Search