Java 25 (LTS) reference · String Methods
Java String formatted() Method
Fill a format string's placeholders such as %s, %d and %.2f with values. Same as String.format().
Java 25 (LTS) Since Java 15
Syntax
string.formatted(args...)
Parameters
| Parameter | Description |
|---|---|
argsObject... |
Values for the % placeholders. |
Return value
A new String.
Examples
public class Main {
public static void main(String[] args) {
String line = "%s ordered %d items for $%.2f".formatted("Ana", 3, 42.5);
System.out.println(line);
System.out.println(String.format("%05d", 42));
}
}
Output
Ana ordered 3 items for $42.50 00042
Run this on your computer — see “Get Started” for setup.
public class Main {
public static void main(String[] args) {
String[][] rows = {{"Mug", "12.00"}, {"Kettle", "34.90"}};
for (String[] r : rows) {
System.out.println("%-8s %8s".formatted(r[0], r[1]));
}
}
}
Output
Mug 12.00 Kettle 34.90
Run this on your computer — see “Get Started” for setup.
Version history
| Version | Change |
|---|---|
| Java Java 15 | Added |
Related
string.repeat()· Return the string repeated n times. Handy for padding, separators and simple text art.String.join()· Join strings or a collection of strings into one string, with a separator between them.- Java Strings · tutorial