Strings beginner

String.formatted()

Call formatted() on the template string itself.

✕ Java 8
String msg = String.format(
    "Hello %s, you are %d",
    name, age
);
✓ Java 15+
String msg =
    "Hello %s, you are %d"
    .formatted(name, age);
📖

Reads naturally

Template.formatted(args) flows better than String.format(template, args).

🔗

Chainable

Can be chained with other string methods.

📏

Less verbose

Drops the redundant String.format() static call.

Old Approach
String.format()
Modern Approach
formatted()
Since JDK
15
Difficulty
beginner
String.formatted()
Available

Widely available since JDK 15 (Sept 2020)

How it works

String.formatted() is an instance method equivalent to String.format() but called on the format string. It reads more naturally in a left-to-right flow.

Share 𝕏 🦋 in