Java Print Output — println, print, printf & Formatting

3 minute read

Printing output to the console is the most basic — and most frequently used — operation in any program. Java gives you several ways to do it.


The Three Print Methods

Java’s System.out object provides three methods for console output:

System.out.println("Hello");  // prints + new line
System.out.print("Hello");    // prints without new line
System.out.printf("Hello %s", "World");  // formatted print

System.out.println()

Prints the argument followed by a newline.

System.out.println("Hello, World!");
System.out.println(42);
System.out.println(3.14);
System.out.println(true);
System.out.println();  // prints a blank line

Output:

Hello, World!
42
3.14
true

Printing Multiple Values

Use string concatenation with +:

String name = "Alice";
int age = 25;
System.out.println("Name: " + name + ", Age: " + age);
// Output: Name: Alice, Age: 25

⚠️ When mixing + with numbers, watch the order:

System.out.println(1 + 2 + " apples");     // "3 apples"
System.out.println("apples " + 1 + 2);     // "apples 12"
System.out.println("apples " + (1 + 2));    // "apples 3"

System.out.print()

Same as println() but does not add a newline.

System.out.print("Hello ");
System.out.print("World");
System.out.println("!");

Output:

Hello World!

Useful for printing on the same line:

for (int i = 1; i <= 5; i++) {
    System.out.print(i + " ");
}
// Output: 1 2 3 4 5

System.out.printf() — Formatted Output

printf() uses format specifiers to control how values are printed.

String name = "Alice";
int age = 25;
double score = 95.678;

System.out.printf("Name: %s, Age: %d, Score: %.2f%n", name, age, score);
// Output: Name: Alice, Age: 25, Score: 95.68

Common Format Specifiers

Specifier Type Example
%s String "hello"
%d Integer (int, long) 42
%f Floating-point 3.140000
%.2f Float with 2 decimals 3.14
%c Character 'A'
%b Boolean true
%n Newline (platform-safe) —
%% Literal % %

Width and Alignment

// Right-aligned (default), width 10
System.out.printf("%10d%n", 42);       //         42

// Left-aligned, width 10
System.out.printf("%-10d|%n", 42);     // 42        |

// Zero-padded, width 5
System.out.printf("%05d%n", 42);       // 00042

Formatting a Table

System.out.printf("%-15s %5s %10s%n", "Product", "Qty", "Price");
System.out.printf("%-15s %5d %10.2f%n", "Coffee", 3, 4.50);
System.out.printf("%-15s %5d %10.2f%n", "Sandwich", 1, 8.99);
System.out.printf("%-15s %5d %10.2f%n", "Water", 2, 1.25);

Output:

Product           Qty      Price
Coffee              3       4.50
Sandwich            1       8.99
Water               2       1.25

String.format()

Same syntax as printf(), but returns a string instead of printing it.

String msg = String.format("Hello, %s! You are %d years old.", "Bob", 30);
System.out.println(msg);
// Output: Hello, Bob! You are 30 years old.

This is useful when you need to build a formatted string for logging, file writing, or other purposes.


Escape Sequences

Special characters in strings:

Sequence Meaning
\n New line
\t Tab
\\ Backslash
\" Double quote
\' Single quote
System.out.println("Line 1\nLine 2");
System.out.println("Column1\tColumn2");
System.out.println("She said \"Hello\"");
System.out.println("Path: C:\\Users\\Alice");

Text Blocks (Java 13+)

For multi-line strings, use text blocks:

String json = """
        {
            "name": "Alice",
            "age": 25
        }
        """;
System.out.println(json);

Text blocks preserve formatting and make multi-line output much cleaner.


System.err.println()

For error messages, use System.err:

System.out.println("This is normal output");
System.err.println("This is an error message");

System.err typically appears in red in IDEs and can be redirected separately from standard output.


Performance Tip: StringBuilder

If you’re building a string inside a loop, don’t use + concatenation — use StringBuilder:

// Bad — creates many intermediate String objects
String result = "";
for (int i = 0; i < 1000; i++) {
    result += i + " ";
}

// Good — efficient
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 1000; i++) {
    sb.append(i).append(" ");
}
System.out.println(sb.toString());

Quick Exercises

  1. Print your name, age, and city on one line using printf.
  2. Print a multiplication table for 5 using printf with aligned columns.
  3. What’s the difference between %n and \n?
  4. Use String.format() to build an email greeting: “Dear [name], your order #[number] is confirmed.”

Summary

Method Newline? Formatting?
println() ✅ ❌
print() ❌ ❌
printf() ❌ (use %n) ✅
String.format() — (returns String) ✅

What’s Next?

You know how to show output — now let’s learn how to read input from the user with Scanner in the next chapter: Take Input.

Updated: