Java Reference
In-Depth Information
We'd all like to think that we never make mistakes. No matter how good a devel-
oper you are, if you write programs then you also write bugs—it's an unavoidable
fact of life. The question isn't whether you'll need to debug your program, but
how you plan on going about doing it. Many beginning programmers rely on
System.out.println() to show them what's happening inside their program, but
more sophisticated developers understand the benefits of using a debugging
tool. The debugger included with IDEA is a powerful, easy-to-use tool for exam-
ining a running program to determine what's happening (or not happening)
behind the scenes. After reading this chapter, you'll never need to rely on print
statements again.
6.1 I ntroducing the debugging process
In this section, we'll introduce the basic concepts of using a symbolic debugger
to analyze Java programs. If you're familiar with using such tools, and you just
want to understand how the IDEA debugger works, feel free to skim ahead to
the next section.
6.1.1
Finding and fixing bugs with a debugger
We must start with some bad news: A debugger can neither find nor fix bugs. If it
could, we'd all have a lot more free time. No, you'll need to find the bugs on your
own, either through testing or pure luck (if you call getting big nasty stack traces
lucky). Although the debugger won't fix bugs for you, it's immensely helpful in
tracking them down and understanding what you must do to fix them.
A debugger is essentially a tool that can tell you what's going on inside the Java
VM as your program is executing. It lets you start your program and then walk
through it, method by method or line by line, using a process known as stepping
through the code . Executing your code this way lets you slow things down and mon-
itor your application's progress. Each step of the way, you can examine the state
and contents of your application's variables and object references. The process
can answer burning questions like, “What is the value of the variable x at the start
of this method?”, “Where did that null pointer exception come from?”, and “How
did I get here?”
Once you suspect a bug is lurking in your code, it's time to bring out the
debugger. Before you begin hunting down bugs, it's important to know exactly
what you're looking for and to have an idea where you may find it. In the wild,
you'll encounter several different species of bugs, and the debugger can help you
track down all of them.
 
 
 
 
 
 
 
 
Search WWH ::




Custom Search