Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I always use System.out.println(...) to debug my code, and it works pretty well. In which cases do you use the eclipse java debugger? I never had to use it and the little bug symbol is still ab bit mysterious to me. Are there cases where the debugger helps me that I can´t solve with a println?

share|improve this question
6  
"What do you mean, you can't solve that with println? println harder!" But seriously, my point is that just because you can solve something with println doesn't mean that it's the easiest, best or fastest way. So what kind of answer are you looking for? Also, don't forget that using print statements means you're constantly modifying code, which 1) takes effort and 2) introduces a possibility of accidental screw-ups. Why not use a logger? And is there a chance that your unit tests aren't as good as they could/should be? –  user39685 Oct 11 '12 at 20:19
    
You should always be using the debugger! –  Bernard Oct 11 '12 at 20:19
2  
programmers.stackexchange.com/questions/78152/… and programmers.stackexchange.com/questions/131377/… already have quite a few opinions on this topic. –  Mat Oct 11 '12 at 20:25
    
@Bernard, your comment is actively useless. Why should he? It's exactly what he's asking. –  Adriano Varoli Piazza Oct 14 '12 at 3:53

6 Answers 6

up vote 8 down vote accepted

Every case...

Granted, I'm not 100% sure about Java IDEs (and would welcome any corrections that aren't consistent with Java IDEs) because I use Visual Studio over C#/F# but adding variables to the watch list and using conditional breaks completely practically replaces println...

  • you don't need to rebuild to get additional information (you don't even need to restart your process/debug session)
  • you can remote debug a process on another machine
  • you can move the current execution while executing (drag the arrow that indicates the current line)
  • step into code/out of/over code based on whether or not you think it matters

And hell, I'm not even that great with the debugger, but it's still light years ahead of console debugging or digging through logs.

share|improve this answer
4  
The most awesome thing I ever learned with the debugger was that you can rewind the stack by dragging and dropping the yellow "next statement" arrow. –  Mike Brown Oct 11 '12 at 20:55
4  
Also: You don't have to spend a frantic evening before a deployment removing all of your println statements. –  FrustratedWithFormsDesigner Oct 11 '12 at 21:22
2  
@Mike Brown: dragging the line indicator is one feature eclipse does not have, but it does have a button to rewind the stack. –  Michael Borgwardt Oct 11 '12 at 21:24
1  
You can also update variables (I don't see this in your list) and (at least in VS) execute methods using the immediate window. This can be quite useful in some situations. –  Daniel B Oct 12 '12 at 11:40
    
Another cool feature is the immediate window, it lets you execute statements and see the output with the current stack. –  Mike Brown Oct 12 '12 at 13:04

The problem with println debugging is that the printlns themselves are a problem.

When you're debugging using println, you have to modify your code to do it. This means you need to already know, or at least have a pretty good idea, where the bug is before you start debugging. And if you already know that, you probably don't need to debug anyway, as (IME at least) the mian reason for debugging is to find where a bug is, not figure out how to fix it. Once you have the bug pinned down, the solution is usually obvious just from the context. (Not always, of course, but usually.)

If your println diagnostics turn out not to give you useful information, or not to give you useful enough information, you need to close the program, change the code, recompile, and then get back to the point you were at to reproduce the bug.

Once you're done with finding and fixing the bug, you then have to change your code again to remove the println statements. (Let's hope you don't miss any and then ship the product with debug output left in!)

An interactive debugger doesn't have these problems. You don't need to know what you're looking for ahead of time; you can examine the values of any arbitrary variable at any time it's in scope. You don't need to modify your code, because the debugger is external to your program. And therefore you don't need to stop debugging, change your code and recompile to look at something new.

That principle alone--inspecting the code from the outside instead of having to instrument it (and then un-instrument it) manually--is the reason interactive debuggers have become the favorite method of debugging among modern programmers.

share|improve this answer

in my experience, the eclipse debugger (or any step-by-step debugger, for that matter) helps a lot more than println statements, because:

  • they don't require several recompiles for what might be a small problem (this might seem like a small problem for small projects, but can quickly escalate for large to very large ones)
  • they often allow debugger-time altering of the variables, which might be handy as well.

(these are just the most basic advantages I found)

However, the debugger is not always that nice:

  • debugging multithreaded programs with a step-by-step debugger is pure hell. In the first writing step, I usually use a logging system with a byte-by-byte debug flag (think global debug, module 1 heavy debug, module 2 heavy debug, ...). It's saved me a lot so far
  • debugging real-time programs where the real-time character is vital to the algorithm (the thing I have in mind was my personal version of face detection, which required the face to move smoothly. It didn't when only getting a frame every couple of seconds because of debugging - granted that this could be solved by taking a video input, just giving an example)

in short: for small, handable projects a debugger is your largest friend. For large projects (which you often work on with a lot of people), an extensive write-time logging is your even larger friend. Or at least mine.

share|improve this answer

I hardly ever find that I need the debugger - but when I need it, I really need it.

One nasty case is where you use println to print out an object, and it prints out "null". After staring at the code for ages, you are convinced that the object cannot be null at that point. And yet, that is what println says. This can waste hours.

In the debugger, you will find immediately that the object is not null. However, the toString() method has been over-ridden to print out the value of some field in that object. The value of this field is, of course, null.

The debugger lets you explore a wider range of questions, more quickly, than using println. But in many simple situations, println is good enough. Just don't waste too much time in those cases when it isn't good enough.

share|improve this answer

You should have googled it first. Debugger in Eclipse already contains a lot of functions that you don't need to define. Other than that, System.out.println() statements might do for you in a simple small project but in a project where many members are working or you have large number of files, you just can't keep adding these statements. It is good to have breakpoints.
I suggest you to take a look at this

share|improve this answer

There isn't really any information a debugger can give you that you could not gain through print statement as well, but...

it's just so much faster and more flexible. Words can't describe the power you get when you can freely look at what the code is doing while it's running.

All in all, I'd estimate that using a debugger typically speeds up the debugging process 10 times. Probably 100 times in cases where there's a lengthy build or deployment between the time where one print statement gives you information about where to next, i.e. where to add another print statement, and the time you can see the output of that new print statement. In a debugger, all it takes is a single click, maybe half a second. If you have to do a full rebuild and deploy to an app server, it could take half an hour.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.