String.formatted()
Call formatted() on the template string itself.
Porównanie kodu
✕ 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ć.
Dlaczego nowoczesne podejście wygrywa
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
Wsparcie JDK
String.formatted()
Dostępne
Widely available since JDK 15 (Sept 2020)
Jak to działa
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.
Powiązana dokumentacja