Debugging with System.out.println
by
, March 10th, 2011 at 12:29 PM (8912 Views)
System.out.prinln is quite possibly the most useful debugging tool there is - especially for the beginner programmer with no knowledge of other tools such as debuggers, assertions, and/or loggers. System.out.println prints to the command line the values passed to the method as an argument (if an object is passed, the String returned by Object.toString() is printed). For debugging, this tool allows the programmer to evaluate a variable value at a certain position in the code. But that's only the beginning - it can be used to evaluate if a conditional is met, how and where a variable is modified, whether a method has been entered, and used with conditionals to more comprehensively inspect value.
Quite often the development process is iterative. Through successive rounds of modifying the code to add or remove print statements, followed by compiling and re-running the code, one can evaluate how the changes affect the printed values. This is invaluable to a beginner - not only in helping one learn the basics through modifying the code and inspecting the outcome, but also in debugging to evaluate which values are expected and which are not, and move forward or backward through the code to see where a problem may lie.
A simple example, find the minimum value
There is an obvious bug in the code above, however for the sake of argument assume we know nothing. The above code prints out 2147483647. Huh...lets use some println's - This value is not even in our array, could it be the value of Integer.MAX_VALUE?
Running the above code assures us that this is indeed Integer.MAX_VALUE - as this is our initial value we initialize min with, it looks that min might never be set - in other words the condition never evaluates to true. Lets go one step further and add a println in our coditional, just to be sure it never evaluates to true
public static void main(String[] args){ int[] values = {10,20,5,3,100,105}; int min = Integer.MAX_VALUE; for ( int i = 0; i < values.length; i++ ){ if ( values[i] > min ){ min = values[i]; System.out.println("MIN HAS BEEN SET"); } } System.out.println("The min value is " + min); System.out.println(Integer.MAX_VALUE); }
Confirms the suspicion that the our coditional is never met. How could this be the case? Looking closely at the conditional, we should (hopefully) see the greater than symbol...don't we want to find the minimum?
public static void main(String[] args){ int[] values = {10,20,5,3,100,105}; int min = Integer.MAX_VALUE; for ( int i = 0; i < values.length; i++ ){ if ( values[i] < min ){ min = values[i]; System.out.println("MIN HAS BEEN SET"); } } System.out.println("The min value is " + min); System.out.println(Integer.MAX_VALUE); }
Aha! From not having a clue where the error lies, to iteratively printing out values and backtracking through the code to determine where the problem resides....System.out.print - the best debugging tool for the beginner
References:
http://download.oracle.com/javase/6/...ng/System.html