W3docs

Java Output: print, println, and printf

Print text and values to the console with System.out.print, println, and printf, and understand the difference between them.

The standard way to print output from a Java program is through System.out, the standard-output stream available to every program. It exposes three methods you'll use constantly: print, println, and printf. They differ only in how they handle newlines and formatting.

System.out.println

println ("print line") writes its argument followed by a newline. Each call moves to the next line:

System.out.println("Line 1");
System.out.println("Line 2");
System.out.println("Line 3");

Output:

Line 1
Line 2
Line 3

This is the method you'll use most.

System.out.print

print is the same as println but without the trailing newline. Useful when you want several values on one line:

System.out.print("Loading");
System.out.print(".");
System.out.print(".");
System.out.print(".");
System.out.println(" done");

Output:

Loading... done

Printing values, not just strings

Both print and println accept any type, not just strings. The runtime converts numbers, booleans, characters, and even objects into a string for you:

System.out.println(42);
System.out.println(3.14);
System.out.println(true);
System.out.println('A');

For arbitrary objects, the runtime calls their toString() method (covered later in OOP).

String concatenation with +

The + operator joins strings — and if you mix in a number or other value, Java converts it automatically:

String name = "Ada";
int age = 36;
System.out.println("Hello, " + name + "! You are " + age + " years old.");

Output:

Hello, Ada! You are 36 years old.

System.out.printf — formatted output

printf ("print formatted") is the Java equivalent of C's printf. You pass a format string with placeholders and the values to substitute:

java— editable, runs on the server

The common placeholders:

  • %s — string
  • %d — integer
  • %f — floating-point number
  • %.2f — float with 2 decimal places
  • %5d — integer right-padded to 5 characters
  • %-5d — integer left-padded to 5 characters
  • %n — platform-specific newline (use this instead of \n in printf)
  • %% — a literal % sign

System.err — printing errors

There's a parallel stream for error output: System.err. It supports the same print, println, and printf methods. By convention, regular output goes to System.out and error messages go to System.err:

System.out.println("Loaded 3 records.");
System.err.println("Warning: could not parse line 5.");

In a terminal both streams appear together, but tools that redirect output (> file.txt) can separate them.

Common mistakes

  • Using print when you wanted println. Output ends up on one line with no separator.
  • Mixing + and , in printf. Format-string placeholders are filled by additional arguments, not by concatenation. Use printf("%s scored %d", name, score), not printf("%s scored", name + score).
  • Forgetting %n in printf. Without it, your output runs together.

What's next

Java Comments shows the three comment syntaxes, including Javadoc — the standard way to document Java code.

Practice

Practice

Which method writes its argument WITHOUT a trailing newline?