How to format strings in Java
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Java offers several ways to format strings, and the best one depends on what you are building. For most code, String.format or printf is enough, while MessageFormat is better when the string is really a user-facing message template rather than just formatted output.
String.format For General Formatting
String.format is the standard choice when you want a formatted string back.
This works much like C-style format strings. %s formats a string and %d formats an integer.
This is a good default when:
- you need alignment or numeric formatting
- you want one clear template string
- you want the result as a
String
printf When You Are Printing Directly
If you are formatting only to print to a stream, printf can be more direct.
The format specifiers are the same idea as String.format, but the output goes directly to the stream instead of returning a string.
Common Format Specifiers
A few specifiers cover most day-to-day work:
- '
%sfor strings' - '
%dfor integers' - '
%ffor floating-point values' - '
%.2ffor floating-point values with two decimals' - '
%nfor a platform-correct newline' - '
%%for a literal percent sign'
Example:
Width And Alignment
Formatting becomes especially useful when you need aligned output.
%-10s left-aligns a string in a width of 10, and %5d right-aligns an integer in a width of 5.
MessageFormat For Template-Like Messages
MessageFormat is a better fit when the string is more like a user-facing message template, especially when localization matters.
This style is easier to externalize for translated or configurable messages than %-based formatting.
String Concatenation Still Has A Place
Not every formatted string needs a formatting API.
For very simple cases, concatenation or interpolation-like building is often clearer than String.format. Do not use formatting syntax when plain string assembly is easier to read.
Formatting Dates And Numbers
For serious locale-aware formatting of numbers, currency, or dates, dedicated classes are often better than generic string formatting.
For example, numeric formatting can use NumberFormat:
This is more appropriate than hardcoding currency symbols into String.format when localization matters.
Common Pitfalls
The most common mistake is using the wrong format specifier, such as %d for a string or %s when numeric formatting was actually needed.
Another mistake is forgetting that %n is preferred over \n in format strings when you want platform-correct newlines.
Developers also overuse String.format for trivial concatenation, which can make simple code harder to read.
Finally, do not use %-style formatting as a substitute for proper locale-aware date, number, and currency formatting when the output is user-facing.
Summary
- Use
String.formatwhen you want a formattedStringresult. - Use
printfwhen you want to write formatted output directly. - Learn the common specifiers such as
%s,%d,%.2f,%n, and%%. - Use
MessageFormatfor message-template style formatting, especially with localization. - Prefer dedicated date and number formatters when locale-sensitive output matters.
Related reading
- How to free memory in Java?
- How to generate a ddl creation script with a modern Spring Boot Data JPA and Hibernate setup?
- How to generate a random permutation in Java
- How to generate a secure random alphanumeric string in Java efficiently?
- How to generate buildConfigField with String type
- How to generate OpenAPI 3.0 YAML file from existing Spring REST API?
- How to generate serial version UID in Intellij
- How to generate Swagger UI from javadocs?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.