Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

View RSS Feed

copeg

Debugging with System.out.println

Rate this Entry
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
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("The min value is " + min);
	    }

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?
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("The min value is " + min);
	    	System.out.println(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

Updated March 11th, 2011 at 09:25 PM by copeg

Categories
Debugging

Comments

  1. JavaPF's Avatar
    permalink
    Great blog post copeg. This knowledge is invaluable to beginners.