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
| Specifier | Type | Example output |
|---|---|---|
%s | String | "Ada" |
%d | int / long | 42 |
%f | float / double | 3.140000 |
%.2f | 2 decimal places | 3.14 |
%05d | zero-padded, width 5 | 00042 |
%,d | thousands separator | 1,000,000 |
%b | boolean | true |
%n | newline | (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.