You can make your programs easier to debug by following the simple
guidelines outlined in this topic.
- Do not hand-tune your source code for performance until you have fully
debugged and tested the untuned version. Hand-tuning might make the logic
of your code harder to understand.
- Where possible, do not put multiple statements on a single line, because
some debugger features operate on a line basis. For example, you cannot step
over or set line breakpoints on more than one statement on the same line.
- Assign intermediate expression values to temporary variables to make it
easier to verify intermediate results by monitoring the temporary variables.
For example, instead of
return new String(myvalue);
write
String newString = new String(myvalue);
return newString;
When you do this, you can look at the contents
of the string you are returning.
If you use javac to compile your code for debugging,
you can set breakpoints and step through your source code without using any
compiler options. However, if you want to examine local, class instance and
static variables while debugging, use the -g compiler
option. For a complete list of compiler options, refer to documentation provided
with the JDK.