
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.
String s = String.format("Hi, %s!", name);
| Call | What 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
public static String format(String format, Object... args);
public static String format(Locale l, String format, Object... args);
Parameters
| Parameter | Description |
|---|
| format | Required. The template with specifiers such as %s. |
| args | Required. The values that fill the specifiers. |
Return Value
| Return Value | Description |
|---|
| New String | The 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.
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
- Store the value.
name holds the text to insert. - Write a template.
"Hi, %s!" marks one string slot. - Call format.
String.format replaces %s with "Ada". - 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.
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
- Pick numeric values.
count is an int; price is a double. - Match specifiers.
%d consumes the int; %.2f formats the double to two places. - Fill left to right. Arguments map to placeholders in order.
- 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.
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);
}
}
Ada [#00042] — 95.5% done
How It Works
- Pass three values. Name, id, and rate fill three conversions.
- Pad the id.
%05d prints 42 as 00042. - One decimal for rate.
%.1f → 95.5. - 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.
CODETOFUN NEWS
Stay Ahead in Tech
Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author
Developer, cloud engineer, and technical writer
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