The String.formatted()
method in Java is used to create a formatted string using the specified arguments. This method is part of the String
class and was introduced in Java 15. It provides a convenient way to format strings similarly to the String.format()
method but in a more fluent and readable manner.
Table of Contents
- Introduction
formatted
Method Syntax- Examples
- Basic Usage
- Using Different Data Types
- Handling Edge Cases
- Real-World Use Case
- Conclusion
Introduction
The String.formatted()
method is a member of the String
class in Java. It allows you to format a string by replacing placeholders with specified arguments. This method is useful for creating readable and maintainable formatted strings.
formatted() Method Syntax
The syntax for the formatted
method is as follows:
public String formatted(Object... args)
- args: The arguments to be formatted into the string.
Examples
Basic Usage
The formatted
method can be used to replace placeholders in a string with the specified arguments.
Example
public class FormattedExample {
public static void main(String[] args) {
String template = "Hello, %s!";
String result = template.formatted("World");
System.out.println(result);
}
}
Output:
Hello, World!
Using Different Data Types
The formatted
method can handle various data types, such as integers, floating-point numbers, and objects.
Example
public class FormattedDataTypesExample {
public static void main(String[] args) {
String template = "Name: %s, Age: %d, Score: %.2f";
String result = template.formatted("John", 25, 85.75);
System.out.println(result);
}
}
Output:
Name: John, Age: 25, Score: 85.75
Handling Edge Cases
Example: Missing Arguments
If the number of arguments provided is less than the number of placeholders, an IllegalFormatException
will be thrown.
public class FormattedMissingArgumentsExample {
public static void main(String[] args) {
try {
String template = "Hello, %s! You are %d years old.";
String result = template.formatted("John");
System.out.println(result);
} catch (IllegalFormatException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Error: Format specifier '%d'
Example: Extra Arguments
If the number of arguments provided is more than the number of placeholders, the extra arguments are ignored.
public class FormattedExtraArgumentsExample {
public static void main(String[] args) {
String template = "Hello, %s!";
String result = template.formatted("John", 25);
System.out.println(result);
}
}
Output:
Hello, John!
Real-World Use Case
Example: Formatting User Information
One common use case for formatted
is to format user information for display or logging.
public class FormatUserInfoExample {
public static void main(String[] args) {
String template = "User: %s, Email: %s, Age: %d";
String userInfo = template.formatted("Alice", "alice@example.com", 30);
System.out.println(userInfo);
}
}
Output:
User: Alice, Email: alice@example.com, Age: 30
In this example, the formatted
method is used to format user information into a readable string.
Conclusion
The String.formatted()
method in Java is a powerful and convenient tool for creating formatted strings using specified arguments. It provides a more fluent and readable alternative to String.format()
. By understanding and utilizing the formatted
method, you can efficiently manage string formatting tasks in your Java programs, making your code more readable and maintainable.