Java String format() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, String.format() builds a new string from a format template and values. Specifiers such as %s, %d, and %.2f are replaced by arguments.

You pass the format string and the values. The format string is required. Call it on the String class. Decimal formatting follows the locale.

Syntax

Call format on the String class. Start with this line.

java
String s = String.format("Hi, %s!", name);
CallWhat it does
String.format("Hi, %s!", name)Builds Hi, name! with the value of name.

Common specifiers: %s for text, %d for integers, %.2f for two decimal places.

Library declarations
java
public static String format(String format, Object... args);
public static String format(Locale l, String format, Object... args);

Parameters

ParameterDescription
formatRequired. The template with specifiers such as %s.
argsRequired. The values that fill the specifiers.

Return Value

Return ValueDescription
New StringThe formatted text.

Java Examples

Example 1 — Insert a Name with %s

The simplest template: one string placeholder. Clearer than building the same line with concat when the wording is fixed.

java
public class FormatBasic {
    public static void main(String[] args) {
        String name = "Ada";

        String msg = String.format("Hi, %s!", name);

        System.out.println(msg);
    }
}

How It Works

  1. Store the value. name holds the text to insert.
  2. Write a template. "Hi, %s!" marks one string slot.
  3. Call format. String.format replaces %s with "Ada".
  4. Use the result. You get a new string to print or return.

Example 2 — Integers and Decimal Precision

Use %d for whole numbers and %.2f for two decimal places— hard to get right with plain + concatenation.

java
public class FormatNumbers {
    public static void main(String[] args) {
        int count = 7;
        double price = 19.5;

        String line = String.format("%d items cost $%.2f", count, price);

        System.out.println(line);
    }
}

How It Works

  1. Pick numeric values. count is an int; price is a double.
  2. Match specifiers. %d consumes the int; %.2f formats the double to two places.
  3. Fill left to right. Arguments map to placeholders in order.
  4. Get money-style output. 19.5 becomes 19.50 with the precision flag.

Example 3 — Mixed Args and Zero-Padding

Combine text, an id pad, and a percent sign. For heavy string building in loops, prefer StringBuilder and format only when you need a final message.

java
public class FormatMixed {
    public static void main(String[] args) {
        String user = "Ada";
        int id = 42;
        double rate = 95.5;

        String line = String.format(
            "%s [#%05d] — %.1f%% done",
            user, id, rate
        );

        System.out.println(line);
    }
}

How It Works

  1. Pass three values. Name, id, and rate fill three conversions.
  2. Pad the id. %05d prints 42 as 00042.
  3. One decimal for rate. %.1f → 95.5.
  4. Escape percent. %% becomes a single % in the output.

Frequently Asked Questions

String.format() builds a new string from a template and values.
Yes. Call String.format(...).
format is required. args are the values that fill the specifiers.
%s for text, %d for integers, and %.2f for decimals.
IllegalFormatException or a related format error is thrown.

Next: formatted()

Fill this string as a format template, like String.format (Java 15+).

formatted() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

8 people found this page helpful