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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.formatted(args...)

Parameters

ParameterDescription
args
Object...
Values for the % placeholders.

Return value

A new String.

Examples

Placeholders
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.

Aligned columns
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

VersionChange
Java Java 15Added

Was this page helpful?