Professional programmers use sophisticated development environments to debug their programs. While these are beyond the scope of this book, we will mention three useful techniques: compiler error messages, System.out.println() statements, and assertions.
Compiler Error Messages
The Java compiler tries to catch as many errors as possible when compiling our programs. It can be annoying to battle the compiler, but it is much better to find errors at this point than to find them when the program is running. We don't have room to discuss all of the different error messages that arise, but there are a few things to keep in mind:
System.out.println() statements
A common question in debugging is, "What's going on at this line?" We may wish to verify that the program reached a certain line or to examine the values of variables at that point. This is easily done by inserting a System.out.println() statement:
System.out.println("Got to point B. x = " + x);
These statements can often produce far too much output. If this becomes a problem, it may be better to print a message only when something unusual happens:
if (x < 0) { System.out.println("Hey, x is negative!"); }
Be sure to remove these debugging statements before turning in your program.
Assertions
Assertions are a debugging feature which first appeared in Java 1.4. They provide a way of formalizing assumptions that we make in our programs. For example, suppose we assume that some variable x is positive. We can add this statement to our program:
assert x >= 0 : "Hey, x is negative!";
This is just like the previous bit of code, except that:
To enable assertions, we must pass the -ea command-line option to Java when we run the program.
java -ea OurProgram
It is common to enable assertions while developing a program, but to disable them once we are fairly confident that the assertions are never violated.
Exercises
A.22 |
Add an assertion to the Circle program (Figure A-17) so that the program crashes if the user enters a negative radius. |
Part I: Object-Oriented Programming
Encapsulation
Polymorphism
Inheritance
Part II: Linear Structures
Stacks and Queues
Array-Based Structures
Linked Structures
Part III: Algorithms
Analysis of Algorithms
Searching and Sorting
Recursion
Part IV: Trees and Sets
Trees
Sets
Part V: Advanced Topics
Advanced Linear Structures
Strings
Advanced Trees
Graphs
Memory Management
Out to the Disk
Part VI: Appendices
A. Review of Java
B. Unified Modeling Language
C. Summation Formulae
D. Further Reading
Index