Java String formatted() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Java 15+

Definition

In Java, template.formatted(args...) returns a new String in which the format specifiers of template (%s, %d, %b, ...) are replaced by the arguments. It is exactly String.format(template, args...), written as an instance call.

The instance form reads naturally at the end of a text block or a constant: "Hi %s".formatted(name). All format syntax, from width and alignment to %% for a percent sign, works the same as with String.format.

formatted() is an instance method of java.lang.String, added in Java 15. Call it on a string, as in text.formatted(...); calling it on a null reference throws NullPointerException. Strings are immutable, so it never changes the original: it returns a new String (or the same one when nothing changes). Keep the result. Requires Java 15 or newer.

Syntax

Call it on the template and pass the values in order.

java
String msg = "Hello, %s! You have %d new messages.".formatted(name, count);
CallWhat it does
"%s-%s".formatted("a", "b")"a-b".
"%5d|".formatted(42)" 42|".
"%d%%".formatted(85)"85%".

formatted always uses the default locale, and there is no overload that takes a Locale. For locale-sensitive output such as decimals with %.2f, call String.format(Locale.ROOT, template, args) instead.

Library declarations
java
public String formatted(Object... args);

Parameters

ParameterDescription
argsThe values for the format specifiers, in order. Extra arguments are ignored.

Return Value

Return ValueDescription
StringThe template with every specifier replaced.

Java Examples

Example 1 — Fill a Message Template

Compare the instance call with the classic static method.

java
public class FormattedBasic {
    public static void main(String[] args) {
        String template = "Hello, %s! You have %d new messages.";

        System.out.println(template.formatted("Ada", 3));
        System.out.println(String.format(template, "Ada", 3));
    }
}

How It Works

  1. Two specifiers, two arguments. %s takes "Ada" and %d takes the integer 3, in order.
  2. The template is the receiver. template.formatted(...) uses the string it is called on as the format.
  3. Same result as String.format. The second line proves both calls produce identical text; the template itself is unchanged.

Example 2 — Print Aligned Columns

Width and alignment flags line up names and numbers.

java
public class FormattedTable {
    public static void main(String[] args) {
        String row = "%-6s|%5d";

        System.out.println(row.formatted("apples", 12));
        System.out.println(row.formatted("kiwis", 7));
        System.out.println(row.formatted("figs", 250));
    }
}

How It Works

  1. Left-aligned text. %-6s pads each name with spaces on the right up to 6 chars.
  2. Right-aligned numbers. %5d pads each number on the left to a width of 5, so the digits line up.
  3. One template, many rows. The same row string is reused for every line.

Example 3 — Format a Text Block and Catch a Type Error

Text blocks and formatted arrived together as standard features in Java 15.

java
import java.util.IllegalFormatException;

public class FormattedTextBlock {
    public static void main(String[] args) {
        String report = """
                Report for %s
                Done: %d%%
                Status: %b
                """.formatted("build #7", 85, true);

        System.out.print(report);

        try {
            "%d items".formatted("three");
        } catch (IllegalFormatException e) {
            System.out.println("wrong type: " + e.getClass().getSimpleName());
        }
    }
}

How It Works

  1. A multi-line template. The text block keeps the three lines and ends with a newline, so print is used.
  2. Escape the percent sign. %d%% prints the number followed by a literal %; %b prints true.
  3. Type errors are caught. %d cannot format the String "three", so IllegalFormatConversionException, a subclass of IllegalFormatException, is thrown.

Frequently Asked Questions

None in the result. template.formatted(args) is the same as String.format(template, args); it is just an instance method that reads better after a text block.
Java 15.
No. formatted() always uses the default locale. Use String.format(Locale, template, args) when you need a specific locale.
Write %% in the template. A single % starts a format specifier.
A subclass of IllegalFormatException, such as MissingFormatArgumentException for too few arguments or IllegalFormatConversionException for a wrong type.

Next: getBytes()

Encode a string into a byte array with a charset.

getBytes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful