JavaOutput & println

Output & println

Almost every Java program you write starts with some form of console output — whether it's a quick debug message or the entire purpose of a small command-line tool. Java gives you three main tools for writing to the console: System.out.println(), System.out.print(), and System.out.printf(). Knowing when to reach for each one — and understanding that there's also a separate System.err stream for error messages — makes your programs both easier to read and easier to debug.
println() vs print()
System.out.println() writes its argument and then appends a newline character, moving the cursor to the next line for whatever gets printed after it. System.out.print() writes the exact same content but leaves the cursor right where the output ends — the next thing printed continues on the same line.

println() vs print()

Java
public class OutputDemo {
    public static void main(String[] args) {
        System.out.println("Line one");
        System.out.println("Line two");

        System.out.print("No newline here... ");
        System.out.print("...so this continues on the same line");
        System.out.println(); // print an empty line to move the cursor down
        System.out.println("Now we're on a fresh line");
    }
}
Line one
Line two
No newline here... ...so this continues on the same line
Now we're on a fresh line
Note
Calling System.out.println() with no arguments is a common trick to print a blank line — it's equivalent to writing just a newline character on its own.
String Concatenation Inside println()
You'll frequently see variables mixed into output using the + operator, which concatenates strings (and converts numbers to their string form automatically along the way).

Concatenating values into a single line

Java
String name = "Ava";
int score = 97;

System.out.println("Player " + name + " scored " + score + " points");
Player Ava scored 97 points
Prefer printf() for formatted output
Concatenation works fine for simple messages, but it gets unwieldy once you need column alignment, decimal precision, or padding. System.out.printf() handles that far more cleanly using format specifiers like %d, %s, and %.2f — see the String Formatting page for the full set of format specifiers and examples.
System.err — a Separate Output Stream
System.err is a second output stream, entirely independent of System.out, intended for error messages and diagnostics. Both streams typically show up together in a terminal, but because they're separate streams, they can be redirected independently — for example, sending normal output to a log file while errors still show up on screen. This separation is also why stack traces printed by uncaught exceptions show up in red in most IDEs: they go through System.err, not System.out.

Separating normal output from error output

Java
public class ErrorStreamDemo {
    public static void main(String[] args) {
        System.out.println("Processing file...");
        System.err.println("Warning: file not found, using defaults");
        System.out.println("Done.");
    }
}
Processing file...
Warning: file not found, using defaults
Done.
Putting It Together

A small worked example mixing all three

Java
public class ReportDemo {
    public static void main(String[] args) {
        String user = "Marcus";
        int attempts = 3;
        boolean success = true;

        System.out.print("User: ");
        System.out.println(user);

        System.out.printf("Attempts: %d, Success: %b%n", attempts, success);

        if (!success) {
            System.err.println("Login failed after " + attempts + " attempts");
        }
    }
}
User: Marcus
Attempts: 3, Success: true

Method

Trailing newline?

Typical use

System.out.print()

No

Building up a line piece by piece

System.out.println()

Yes

Most everyday output — one message per line

System.out.printf()

Only if %n/\n is included

Formatted, aligned, or precision-controlled output

System.err.println()

Yes

Error messages and diagnostics, kept separate from normal output

  • println() adds a trailing newline; print() does not

  • The + operator concatenates strings and auto-converts numbers

  • printf() is the better tool once formatting gets specific — see String Formatting

  • System.err is a distinct stream, conventionally reserved for errors and diagnostics