The String.format()
method in Java is a powerful tool for creating formatted strings. Think of it as a way to insert variables into a string template, just like you might use placeholders in a word processor or a mail merge application.
Syntax
public static String format(Locale l, String format, Object... args)
public static String format(String format, Object... args)
The format()
method has two main overloads. The first allows you to specify a locale for formatting (useful for things like currency symbols or date formats). The second is more commonly used and just requires the format string and the arguments.
Parameters
Parameter | Description |
l (optional) | The locale to use for formatting. If omitted, the default locale is used. |
format | A format string that specifies how the arguments should be formatted within the resulting string. This string contains plain text and format specifiers (e.g., %d for integers, %f for floating-point numbers). |
args | An array of objects to be inserted into the format string. The types of these objects must be compatible with the format specifiers in the format string. |
Return Value
The format()
method returns a new string that is formatted according to the specified pattern and arguments.
Examples
Example 1: Basic Integer Formatting
This example demonstrates how to format an integer using the %d
specifier. The %d
specifier tells Java to insert a decimal (integer) value into the string.
int age = 30;
String formattedAge = String.format("You are %d years old.", age);
System.out.println(formattedAge);
You are 30 years old.
Explanation: The value of the age
variable (30) is inserted into the format string at the location of the %d
placeholder.
Example 2: Formatting Floating-Point Numbers
This example shows how to format a floating-point number using the %f
specifier. The %f
specifier is used for inserting floating point numbers, and you can control the precision (number of digits after the decimal point).
double price = 19.99;
String formattedPrice = String.format("The price is $%.2f.", price);
System.out.println(formattedPrice);
The price is $19.99.
Explanation: The %.2f
format specifier tells Java to insert the value of price
as a floating-point number with two digits after the decimal point.
Example 3: Formatting Strings
This example illustrates how to incorporate strings into formatted output using the %s
specifier. The %s
is used for inserting String values.
String name = "Alice"
String greeting = String.format("Hello, %s!", name);
System.out.println(greeting);
Hello, Alice!
Explanation: The value of the name
variable ("Alice") is inserted into the format string at the location indicated by %s
.
Example 4: Using Multiple Arguments
This example demonstrates formatting with multiple arguments. You can use several specifiers in a single format string.
String firstName = "John"
String lastName = "Doe"
int id = 12345;
String formattedInfo = String.format("Name: %s %s, ID: %d.", firstName, lastName, id);
System.out.println(formattedInfo);
Name: John Doe, ID: 12345.
Explanation: The format string contains three placeholders (%s, %s, and %d), and their corresponding values are taken from the firstName
, lastName
, and id
variables respectively.
Example 5: Using Locale for Currency Formatting
This example shows how to format a currency value using different locales. This is particularly useful when you need to display amounts in various currencies with the correct symbols and formats (e.g., US dollars vs. Euros).
double amount = 1000.00;
Locale usLocale = Locale.US; // For US Dollars
Locale germanLocale = new Locale("de", "DE"); // For German Euros
String usdFormatted = String.format(usLocale, "$%.2f", amount);
String eurFormatted = String.format(germanLocale, "%.2f €", amount);
System.out.println("US Dollar: " + usdFormatted);
System.out.println("German Euro: " + eurFormatted);
US Dollar: $1000.00
German Euro: 1000,00 €
Explanation: We use the Locale
to specify the currency format. The US locale displays a dollar sign ($) before the amount, while the German locale includes the euro symbol (€) after the amount and uses a comma as the decimal separator.