Hello, I am new to java but have been programming for a while in other languages. Usually when debugging I make heavy use of a MsgBox (from Visual Basic) or something like that -- a way to send myself messages as the code is running. Is there anything like this is java? Thanks! Karen.
For simple debugging you can just use some System.out.println statements at relevant places in your.
If you need more detail about the state of your application many IDE's have powerful debuggers which enable step by step analysis of all aspects of your application as it is running - personally I use the Eclipse debugger heavily.
Well, there's System.out.println(), and javax.swing.JOptionPane, and there are several nice logging APIs, but none of this really qualifies as "debugging". There's a command-line debugger JDB for that, and there are a number of excellent graphical debuggers -- most every Java IDE has one -- that let you step through source, set breakpoints, inspect variables, etc.
Test-driven development is an excellent alternative to debugging with print statements. Agile Java (Langr) is a book (none finer) that covers that in detail and will put you on the right path for Java development.
Meanwhile, try using assert statements to "debug" your code. They are very simple to use and very effective. You can use them to output values in context, just like MsgBox:
When you run java you have to enable asserts: java -ea ....
Easy, effective, and definitely conforms to best practices for debugging. IDE debugging (setting breakpoints and such, as you could do in VB) is also indespensible at times.
I guess most of you are lucky to have IDEs and are able to do "interactive" debugging. I don't have any fancy tools and am stuck with println()s and have been using them for years. I think it qualifies as "debugging".
Joined: Sep 19, 2004
I think that you have a lot of programmers who wait for bugs to be reported then fix them in the IDE. That is to say, they do not use defensive coding first and foremost: assert, unittest. They also don't do a lot of logging even though it's a frequent requirement on a deliverables list.
I just love the IDE for fixing bugs that were not caught in tests or are not obvious in the logs. Wonderful tool, but not the key to debugging the code.
We seem to be talking about a couple of different kinds of debugging here. System.out.println is great when the code doesn't run and you can't figure out what's going wrong with it. Even with the IDE debugger, it is useful to narrow down where the problem is so you can see where to put a breakpoint. I use it a lot sometimes.
However, when the code is running and you're coming up with the wrong output, I would have to agree with Rick; assert, unit test (JUnit, etc), and log (log4j is pretty good in my experience) to prevent/fix bugs.
JavaBeginnersFaq "Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Joined: Aug 19, 2005
Hi, this is Karen again. Thanks for all the replies! I had forgotten that I can use the command line and Swing at the same time. I don't use an IDE (yet anyway) so that's out. And usually I'm the end user of my programs, so I haven't gotten into more serious debugging much. Lots to learn! -Karen.