Java:
Printing debug output

How to:

Let’s get some code on the screen:

public class DebugExample {
    public static void main(String[] args) {
        int sum = 0;
        for (int i = 1; i <= 10; i++) {
            sum += i;
            System.out.println("Added " + i + ", sum now: " + sum);
        }
    }
}

This snippet sums numbers from 1 to 10 and prints progress:

Added 1, sum now: 1
Added 2, sum now: 3
...
Added 10, sum now: 55

Deep Dive

Back before IDEs got smart, printf-style debugging was the go-to. Even now, amidst fancy breakpoints, sometimes a well-placed System.out.println() is all you need to align the planets.

Alternatives? Logging frameworks like Log4J or SLF4J give you control over debug info, separating it from system output and letting you toggle verbosity.

Implementation-wise, remember that System.out is a PrintStream object, defaulting to stdout. It can be replaced to redirect output, making testing or logging less intrusive.

See Also