Java Reference
In-Depth Information
3.40
Is (x > 0 && x < 10) the same as ((x > 0) && (x < 10)) ? Is (x > 0 ||
x < 10) the same as ((x > 0) || (x < 10)) ? Is (x > 0 || x < 10 && y
< 0) the same as (x > 0 || (x < 10 && y < 0)) ?
3.16 Debugging
Debugging is the process of finding and fixing errors in a program.
Key
Point
As mentioned in Section 1.10.1, syntax errors are easy to find and easy to correct because the
compiler gives indications as to where the errors came from and why they are there. Runtime
errors are not difficult to find either, because the Java interpreter displays them on the console
when the program aborts. Finding logic errors, on the other hand, can be very challenging.
Logic errors are called bugs . The process of finding and correcting errors is called
debugging . A common approach to debugging is to use a combination of methods to help
pinpoint the part of the program where the bug is located. You can hand-trace the program
(i.e., catch errors by reading the program), or you can insert print statements in order to show
the values of the variables or the execution flow of the program. These approaches might work
for debugging a short, simple program, but for a large, complex program, the most effective
approach is to use a debugger utility.
JDK includes a command-line debugger, jdb, which is invoked with a class name. jdb is
itself a Java program, running its own copy of Java interpreter. All the Java IDE tools, such
as Eclipse and NetBeans, include integrated debuggers. The debugger utilities let you follow
the execution of a program. They vary from one system to another, but they all support most
of the following helpful features.
bugs
debugging
hand-traces
Executing a single statement at a time: The debugger allows you to execute one
statement at a time so that you can see the effect of each statement.
Tracing into or stepping over a method: If a method is being executed, you
can ask the debugger to enter the method and execute one statement at a time in the
method, or you can ask it to step over the entire method. You should step over the
entire method if you know that the method works. For example, always step over
system-supplied methods, such as System.out.println .
Setting breakpoints: You can also set a breakpoint at a specific statement. Your
program pauses when it reaches a breakpoint. You can set as many breakpoints as
you want. Breakpoints are particularly useful when you know where your program-
ming error starts. You can set a breakpoint at that statement and have the program
execute until it reaches the breakpoint.
Displaying variables: The debugger lets you select several variables and display
their values. As you trace through a program, the content of a variable is continuously
updated.
Displaying call stacks: The debugger lets you trace all of the method calls. This
feature is helpful when you need to see a large picture of the program-execution flow.
Modifying variables: Some debuggers enable you to modify the value of a vari-
able when debugging. This is convenient when you want to test a program with dif-
ferent samples but do not want to leave the debugger.
Tip
If you use an IDE such as Eclipse or NetBeans, please refer to Learning Java Effectively
with Eclipse/NetBeans in Supplements II.C and II.E on the Companion Website. The
supplement shows you how to use a debugger to trace programs and how debugging
can help in learning Java effectively.
debugging in IDE
 
 
 
Search WWH ::




Custom Search