Take the 2-minute tour ×

In PHP, I always just var_dump'd everything. In Java, when I'm debugging a program such as implement BFS. I am finding it extremely difficult to just System.out.println everything, especially when there are tons of loops.

I think I have a poor practice and workflow. I could use JUnit, but when I have to go through every step to make sure every little thing is true or false and if variables are setting properly and checking each and every section, I am not sure if JUnit is any faster than System.out.println.

I am sure I might be doing something wrong, so I wanted to ask the community for better practices.

Edit: I use Eclipse, just to let you guys know. However, I don't know how to utilize every aspect of it. I just write code, debug, and compile. Sometimes JUnit, when I am required to use it.

share|improve this question
For debugging, use a debugger. For unit testing, use a unit testing framework. They're different things. –  Roger Lipscombe Dec 17 '11 at 8:46
and use logger for logging –  Oleg Mikheev Dec 17 '11 at 9:37
@pst Thanks for title change! –  Strawberry Dec 17 '11 at 12:37
add comment

4 Answers

up vote 3 down vote accepted

I suggest you start with "Using the eclipse debugger"

share|improve this answer
and why eclipse? –  Oleg Mikheev Dec 17 '11 at 9:38
It isn't that he should use eclipse for debugging (I personally prefer Intellij). It is that the videos present the notion of a debugger and its use which is what Doug asked about –  Arnon Rotem-Gal-Oz Dec 17 '11 at 12:39
add comment

Instead of just printing debug information to the console, use a logging system like log4j.

This offers dynamic loglevels and logging can be send to a set of rotating files through configuration. (I.e. log lots of data while developing, and only log the errors in production without having to change the course code.)

share|improve this answer
add comment

I'm still quite new to Java coding, but I've found it very helpfully to create an Debug class and calling it instead of doing a lot of System.out.println calls.

My debuging class look something like this..


   public class Debug {
    private static boolean debugActive = true;
    public static void setDebug(boolean debug) {
        Debug.debugActive = debug;

    public static void print(String str) {
        if (debugActive) {

            StackTraceElement[] stacktrace = Thread.currentThread().getStackTrace();
            StackTraceElement e = stacktrace[2];
            String methodName = e.getMethodName();

            System.out.print("[" + e + "]");
            System.out.print(" => " + str);
            System.out.println(" <= ");


    public static void print(int x) {
        print(x + "");

So calling it from the program

DbIniParser.java ..


Main program.

DbIniParser ini = new DbIniParser();

This provides a output as..

Console output

[hospitalcase.DbIniParser.readFile(DbIniParser.java:35)] => hjess <= 
[hospitalcase.DbIniParser.readFile(DbIniParser.java:36)] => password <= 
[hospitalcase.DbIniParser.readFile(DbIniParser.java:37)] => myFunnyDatabase <= 
[hospitalcase.DbIniParser.readFile(DbIniParser.java:38)] => localhost <= 
[hospitalcase.DbIniParser.readFile(DbIniParser.java:39)] => 3306 <= 

By using a class itself the output could be changed to be a log file or something else.

Please mind I've only done programming in 3 months so this way might not be optimal, but I've found it quite useful instead for doing System.out.print every time.

share|improve this answer
What you are doing is very similar to what logging frameworks do. Consider checking, for example, log4j –  Miquel Nov 28 '12 at 20:03
You should have a look at Log4J, certainly the most used logging framework in Java. It provides everything you have in your Debug class, and a lot more. –  Florent Bayle Nov 28 '12 at 20:04
Thanks - I quite new to Java so thanks for all the tips –  HJess Dec 16 '12 at 12:48
add comment

I strongly recommend to use an IDE such as NetBeans or Eclipse. They come with a debugger.

share|improve this answer
add comment

Your Answer


