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);
Console output

Vardas: Ieva, taškai: 18

  1. sablonas stores text with two format specifiers.
  2. %s is a placeholder for text.
  3. %d is a placeholder for a decimal integer.
  4. formatted("Ieva", 18) supplies values in the same order as the template's placeholders.
  5. 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);
Console output

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)

Parts of the formatted() method
PartMeaning
Calling objectA text template, for example, "Sveikas, %s!"
Parameterargs – values to insert into format specifiers
Parameter typeObject..., allowing a variable number of values
Return typeString
Java versionAvailable since Java 15
ErrorsIllegalFormatException 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);
Console output

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

Common formatted() specifiers
SpecifierMeaningExample
%sText or an object's string representation"Ieva"
%dDecimal integer18
%fFloating-point number3.500000
%.2fFloating-point number with two decimal places3.50
%bBoolean valuetrue
%%A literal percent sign%
%nA platform-specific newlineLine break

Decimal places

double kaina = 3.5;
String tekstas = "Kaina: %.2f Eur".formatted(kaina);

System.out.println(tekstas);
Console output

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.

Locale depends on the environment.

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);
Console output

|Duona     |   2|
|Obuoliai   |  12|

  • %-10s reserves ten positions for left-aligned text.
  • %4d reserves 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);
Console output

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);
Console output

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);
Console output

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);
Result

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);
Result

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 Locale directly to formatted()when that requires String.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.