Strings Początkujący

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);
Widzisz problem z tym kodem? Daj nam znać.
📖

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.

Stare podejście
String.format()
Nowoczesne podejście
formatted()
Od JDK
15
Poziom trudności
Początkujący
String.formatted()
Dostępne

Widely available since JDK 15 (Sept 2020)

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.

Udostępnij 𝕏 🦋 in