Java
String Formatting
Master String formatting using printf, String.format(), and Text Blocks.
By TechCoder TeamLast updated: 2026-06-02
In a Nutshell
Master String formatting using printf, String.format(), and Text Blocks. This hands-on tutorial focuses on practical implementation of string formatting concepts.
String Formatting
Java provides several ways to format strings.
String.format()
Returns a formatted string.
String name = "Alice";
int age = 25;
String result = String.format("My name is %s and I am %d years old.", name, age);
Common Format Specifiers
%s- String%d- Integer (decimal)%f- Floating point number%b- Boolean%n- Newline (platform unsafe)
System.out.printf()
Prints a formatted string directly to console.
System.out.printf("Value of Pi: %.2f%n", Math.PI); // 3.14
String.join() (Java 8)
Joins strings with a delimiter.
String languages = String.join(", ", "Java", "Python", "C++");
// "Java, Python, C++"
AI Mentor
Confused about "Java string formatting techniques"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 3Which specifier is used for floating point numbers?
%d
%f
%s