CS Resources—Java; Debugging Tutorial


How to Compile and Run a Java Program in Unix

How to Debug a Java Program

Once your Java program has compiled you may need to debug it. The most important things you need to do is to find out the values of variables in your code and monitor the execution of the code, particularly the calling of procedures and functions.

The java System.out.println statement is and effective way to debug a program. Placing "debugging printouts" throughout the program at critical places to monitor the execution can be helpful. These printouts can easily be commented out when the debugging is completed, and later uncommented if further bugs show up later.

But what to do when this becomes too tedious? Another option is to use a debugger, such as jdb. We'll go through an example which takes you through some basic debugging tasks. This tutorial goes through using the text version of the debugger. At the completion of the tutorial you should be comfortable enough with the different debugging techniques. For those students that want the debugger to be a little more visual, there is a further tutorial that teaches you how to integrate jdb and Emacs together into a development tool (see the bottom of this tutorial).

Test Your Debugging Skills

Now it's time to try your hand at debugging some code. We will now look at the fib.java program which produces the desired Fibonacci number using the same recursive method as the last program. However, there are two errors in the program. The first is a bug that prevents the program from properly compiling, and the other is a logic error in the code.


Solution to compile error! To make the solution appear, highlight the space under this text.

The compile error has to deal with the fact that the class is not properly named the same as the file name. The class should be called fib, not fact.

Hint to logic error! To make the hint appear, highlight the space under this text.

What are the values of the parameters being recursively passed into the Fibonacci method? Will these calls to Fibonacci ever terminate?