How to Print in Java: println, print, printf

The three core print methods in Java are System.out.println (prints with a newline), System.out.print (prints without a newline), and System.out.printf (prints with format specifiers like C's printf).

println β€” print with newline

System.out.println("Hello, world!");   // Hello, world! + newline
System.out.println(42);                // 42 + newline
System.out.println(3.14);             // 3.14 + newline
System.out.println(true);             // true + newline
System.out.println();                  // blank line only

ln stands for "line" β€” it appends the system line separator (\n on Unix, \r\n on Windows) after the value.

print β€” print without newline

System.out.print("Hello");
System.out.print(", ");
System.out.print("world!");
// Output: Hello, world! (on one line, no trailing newline)

printf β€” formatted output

String name = "Ada";
int age = 36;
double salary = 75000.5;

System.out.printf("Name: %s%n", name);             // Name: Ada
System.out.printf("Age:  %d%n", age);              // Age:  36
System.out.printf("Salary: $%,.2f%n", salary);     // Salary: $75,000.50
System.out.printf("%-10s | %5d%n", name, age);     // Ada        |    36 (aligned)

Use %n as the newline in printf instead of \n β€” it uses the platform-correct line separator.

Common format specifiers

SpecifierTypeExample output
%sString"Ada"
%dint / long42
%ffloat / double3.140000
%.2f2 decimal places3.14
%05dzero-padded, width 500042
%,dthousands separator1,000,000
%bbooleantrue
%nnewline(newline)
%%literal %%

formatted() β€” Java 15+

String msg = "Hello, %s! You have %d messages.".formatted("Ada", 3);
System.out.println(msg);
// Hello, Ada! You have 3 messages.

Print to stderr

System.err.println("Error: file not found"); // stderr, typically red in IDEs

Printing objects

List<Integer> list = List.of(1, 2, 3);
System.out.println(list);  // [1, 2, 3] β€” calls list.toString()

Any object's toString() is called automatically. Override it in your own classes for readable output.