String Formatting
Simple + concatenation works fine for a quick message, but once you need aligned columns, controlled decimal precision, or the same message built from different values repeatedly, Java's formatted output tools become far more readable and maintainable.
String.format() and printf-Style Specifiers
String.format() builds a formatted String using a template with placeholders called format specifiers. System.out.printf() works exactly the same way, except it prints the result directly instead of returning it.
Specifier | Meaning |
%d | Integer value |
%s | String value (works with almost any type) |
%f | Floating-point value |
%.2f | Floating-point value, 2 digits after the decimal |
%n | Platform-independent newline |
String.format and printf
Java
public class FormatDemo {
public static void main(String[] args) {
String name = "Aman";
int age = 28;
double price = 19.999;
String message = String.format("Name: %s, Age: %d", name, age);
System.out.println(message);
System.out.printf("Price: $%.2f%n", price);
System.out.printf("%s is %d years old.%n", name, age);
}
}Name: Aman, Age: 28 Price: $20.00 Aman is 28 years old.
Note
%.2f rounds to 2 decimal places — notice 19.999 became 20.00, not 19.99. The number after the dot in a float specifier controls precision, and Java rounds rather than truncates.
Tip
Use %n instead of the literal \n inside format strings. %n produces the correct newline sequence for whatever operating system the program is running on, while \n always produces a Unix-style newline regardless of platform.
Padding and Aligning Output
Format specifiers also support width and alignment, which is especially useful for printing tabular data in the console.
Column alignment with width specifiers
Java
public class TableFormatDemo {
public static void main(String[] args) {
System.out.printf("%-10s%5s%n", "Item", "Qty");
System.out.printf("%-10s%5d%n", "Apples", 12);
System.out.printf("%-10s%5d%n", "Bananas", 7);
}
}Item Qty Apples 12 Bananas 7
Note
%-10s left-aligns a string within a 10-character field; %5d right-aligns an integer within a 5-character field. Combining these is the standard trick for producing readable, column-aligned console output.
Text Blocks: A Modern Alternative for Multi-Line Strings
When you need a large, multi-line chunk of text — an HTML snippet, a JSON payload, a SQL query — manually concatenating lines with + and \n gets messy fast. Java 15+ introduced text blocks, a cleaner way to write multi-line string literals directly in your source code. They're covered in full on the dedicated Text Blocks page, but here's a quick preview:
A text block (Java 15+)
Java
public class TextBlockPreviewDemo {
public static void main(String[] args) {
String html = """
<html>
<body>
<p>Hello, World!</p>
</body>
</html>
""";
System.out.println(html);
}
}Choosing Between + and Formatted Output
Situation | Recommended Approach |
One or two values, simple message |
|
Controlling decimal precision | String.format() / printf with %.Nf |
Aligned, tabular console output | printf with width specifiers |
Large multi-line text | Text blocks |
Tip
+ concatenation is perfectly fine — and often more readable — for simple, one-off messages. Reach for String.format() or printf once you need precision control, alignment, or the same template applied to many different values.