STRING INSTANCE METHODS
Java String formatted() method
formatted() formats text using the String object itself as the template. The template contains format specifiers, and the arguments provide the values to insert.
Your first formatted() example
String sablonas = "Vardas: %s, taškai: %d";
String tekstas = sablonas.formatted("Ieva", 18);
System.out.println(tekstas);Vardas: Ieva, taškai: 18
sablonasstores text with two format specifiers.%sis a placeholder for text.%dis a placeholder for a decimal integer.formatted("Ieva", 18)supplies values in the same order as the template's placeholders.- The method returns a new formatted
String.
Why is formatted() an instance method?
formatted() is called on a specific string because that string is the formatting template:
"Sveikas, %s!".formatted("Tomai")
String zinute = "Sveikas, %s!".formatted("Tomai");
System.out.println(zinute);Sveikas, Tomai!
As a result, formatted() is convenient for short templates: the template stays on the left, with its values immediately beside it.
Method syntax
public String formatted(Object... args)
| Part | Meaning |
|---|---|
| Calling object | A text template, for example, "Sveikas, %s!" |
| Parameter | args – values to insert into format specifiers |
| Parameter type | Object..., allowing a variable number of values |
| Return type | String |
| Java version | Available since Java 15 |
| Errors | IllegalFormatException errors when the template and arguments do not match |
The three dots Object... means the method accepts zero, one or more arguments.
formatted() and String.format()
String sablonas = "%s mokosi %s";
String pirmas = sablonas.formatted("Rūta", "Java");
String antras = String.format(sablonas, "Rūta", "Java");
System.out.println(pirmas);
System.out.println(antras);Rūta mokosi Java
Rūta mokosi Java
formatted() is a convenient form when the template is already a String object. According to the Java API, it behaves like String.format(this, args), using the same specifiers and formatting rules.
Common format specifiers
| Specifier | Meaning | Example |
|---|---|---|
%s | Text or an object's string representation | "Ieva" |
%d | Decimal integer | 18 |
%f | Floating-point number | 3.500000 |
%.2f | Floating-point number with two decimal places | 3.50 |
%b | Boolean value | true |
%% | A literal percent sign | % |
%n | A platform-specific newline | Line break |
Decimal places
double kaina = 3.5;
String tekstas = "Kaina: %.2f Eur".formatted(kaina);
System.out.println(tekstas);Kaina: 3.50 Eur
Specifier %.2f means a floating-point number with two decimal places. Formatting changes only the text representation, not the numeric value used to create it.
formatted() has no Localeparameter. To select a locale explicitly, use String.format(Locale, format, args).
Width and alignment
String pirma = "|%-10s|%4d|".formatted("Duona", 2);
String antra = "|%-10s|%4d|".formatted("Obuoliai", 12);
System.out.println(pirma);
System.out.println(antra);|Duona | 2|
|Obuoliai | 12|
%-10sreserves ten positions for left-aligned text.%4dreserves four positions for a right-aligned number.- The vertical bars are not format specifiers: they make the field boundaries easier to see.
A percent sign and newline
String tekstas = "Nuolaida: 20%%%nLiko: %d vnt.".formatted(4);
System.out.print(tekstas);Nuolaida: 20%
Liko: 4 vnt.
One % starts a format specifier, so a literal percent sign uses %%. The specifier %n creates a newline according to the operating system's rules.
Changing argument order
String tekstas = "%2$s turi %1$d taškų".formatted(42, "Mantas");
System.out.println(tekstas);Mantas turi 42 taškų
A specifier can include an argument number. %2$s uses the second argument as text, while %1$d uses the first as an integer. This helps when word order differs between languages.
Extra arguments are ignored
String tekstas = "Sveikas, %s!".formatted("Jonai", 2026);
System.out.println(tekstas);Sveikas, Jonai!
If there are more arguments than placeholders, the extras are not inserted. This is not an error, but often indicates an unintended template or call.
A missing argument causes an error
String tekstas = "%s gavo %d taškų".formatted("Ieva");
System.out.println(tekstas);MissingFormatArgumentException
The template has two placeholders but only one argument. Java cannot fill the %d position, so it throws a formatting error at runtime.
The specifier must fit the argument
String tekstas = "Kiekis: %d".formatted("penki");
System.out.println(tekstas);IllegalFormatConversionException
%d expects an integer. Passing text instead creates a template-argument mismatch, so Java throws a formatting error.
Common mistakes
- Forgetting that
formatted()is available since Java 15. - Specifiers and arguments are in different orders.
- A specifier receives an argument of the wrong type.
- Writing a percent sign as
%instead of%%. - Expecting to pass
Localedirectly toformatted()when that requiresString.format(Locale, ...). - Using formatting as a substitute for calculation when it only changes the result's presentation.
When should you use formatted()?
formatted() Use it for readable templates filled with values: messages, errors, tables, short reports or test output. For explicit control over Locale, choose String.format(Locale, ...). For a few simple text parts, the + operator or this may suffice: StringBuilder.