In Java, template.formatted(args...) returns a new String in which the format specifiers of template (%s, %d, %b, ...) are replaced by the arguments. It is exactly String.format(template, args...), written as an instance call.
The instance form reads naturally at the end of a text block or a constant: "Hi %s".formatted(name). All format syntax, from width and alignment to %% for a percent sign, works the same as with String.format.
formatted() is an instance method of java.lang.String, added in Java 15. Call it on a string, as in text.formatted(...); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 15 or newer.
Syntax
Call it on the template and pass the values in order.
java
String msg = "Hello, %s! You have %d new messages.".formatted(name, count);
Call
What it does
"%s-%s".formatted("a", "b")
"a-b".
"%5d|".formatted(42)
" 42|".
"%d%%".formatted(85)
"85%".
formatted always uses the default locale, and there is no overload that takes a Locale. For locale-sensitive output such as decimals with %.2f, call String.format(Locale.ROOT, template, args) instead.
Library declarations
java
public String formatted(Object... args);
Parameters
Parameter
Description
args
The values for the format specifiers, in order. Extra arguments are ignored.
Return Value
Return Value
Description
String
The template with every specifier replaced.
Java Examples
Example 1 — Fill a Message Template
Compare the instance call with the classic static method.
java
public class FormattedBasic {
public static void main(String[] args) {
String template = "Hello, %s! You have %d new messages.";
System.out.println(template.formatted("Ada", 3));
System.out.println(String.format(template, "Ada", 3));
}
}
Output
Hello, Ada! You have 3 new messages.
Hello, Ada! You have 3 new messages.
How It Works
Two specifiers, two arguments.%s takes "Ada" and %d takes the integer 3, in order.
The template is the receiver.template.formatted(...) uses the string it is called on as the format.
Same result as String.format. The second line proves both calls produce identical text; the template itself is unchanged.
Example 2 — Print Aligned Columns
Width and alignment flags line up names and numbers.
java
public class FormattedTable {
public static void main(String[] args) {
String row = "%-6s|%5d";
System.out.println(row.formatted("apples", 12));
System.out.println(row.formatted("kiwis", 7));
System.out.println(row.formatted("figs", 250));
}
}
Output
apples| 12
kiwis | 7
figs | 250
How It Works
Left-aligned text.%-6s pads each name with spaces on the right up to 6 chars.
Right-aligned numbers.%5d pads each number on the left to a width of 5, so the digits line up.
One template, many rows. The same row string is reused for every line.
Example 3 — Format a Text Block and Catch a Type Error
Text blocks and formatted arrived together as standard features in Java 15.
A multi-line template. The text block keeps the three lines and ends with a newline, so print is used.
Escape the percent sign.%d%% prints the number followed by a literal %; %b prints true.
Type errors are caught.%d cannot format the String "three", so IllegalFormatConversionException, a subclass of IllegalFormatException, is thrown.
Frequently Asked Questions
None in the result. template.formatted(args) is the same as String.format(template, args); it is just an instance method that reads better after a text block.
Java 15.
No. formatted() always uses the default locale. Use String.format(Locale, template, args) when you need a specific locale.
Write %% in the template. A single % starts a format specifier.
A subclass of IllegalFormatException, such as MissingFormatArgumentException for too few arguments or IllegalFormatConversionException for a wrong type.