Learn Java
import com.learnjava.ioformatting.StringFormatting;

String Formatting (String.format, printf) in Java

@Concept

String.format and System.out.printf both build formatted text from a template string containing placeholders — %s for a string, %d for an integer, %f for a floating-point number, and several more — substituting each placeholder with the corresponding argument in order. The difference between the two is just where the result goes: String.format returns the formatted text as a String to store or pass around, while printf writes it straight to standard output.

Placeholders support extra formatting details inside them — %.2f rounds a decimal to two places, %5d pads a number to a minimum width of 5 characters, and %-10s left-aligns a string inside a 10-character field. That's precise alignment and rounding that plain string concatenation can't do cleanly.

@HowItWorks
1

The basic placeholders

%s for a String, %d for an integer, %f for a floating-point number, each replaced by the matching argument in order.

2

String.format vs printf

String.format returns the formatted text as a String; printf writes that same formatted text directly to standard output.

3

Controlling decimal places

%.2f rounds a floating-point value to exactly two digits after the decimal point.

4

Controlling width and alignment

%5d pads a number to at least 5 characters wide, and a minus sign like %-10s left-aligns instead of the default right-alignment.

5

Multiple placeholders, one call

A single format string can contain several placeholders, filled in order by however many arguments follow it.

@Example
public class FormatDemo {
public static void main(String[] args) {
String message = String.format("Name: %-10s Score: %6.2f", "Ada", 95.5);
System.out.println(message);
System.out.printf("Total: %d items%n", 3);
}
}
Output
Name: Ada Score: 95.50
Total: 3 items
1%-10s left-aligns "Ada" inside a 10-character field, padding the rest with spaces.
2%6.2f reserves 6 characters total for 95.5, rounded to exactly two decimal places, so it prints as " 95.50" with a leading space to fill the width.
3String.format builds that whole line as a String first, which println then prints as one line.
4printf works directly, filling %d with 3 and %n with a platform-correct newline, producing the second line.
@UseCases
Aligning a table of names and numbers into neat columns in console output
Rounding a price or percentage to exactly two decimal places for display
Building a formatted String to reuse elsewhere, not just print immediately
Padding numbers with leading characters for fixed-width output like invoice or ID numbers

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.