🌱BeginnerWorking with Strings
String Formatting
Format numbers, text, and dates precisely using String.format(), printf(), and formatted() with format specifiers.
Overview
String formatting lets you control exactly how values appear in output - decimal places, padding, alignment, and more. Java provides System.out.printf() for direct formatted printing, String.format() to build a formatted String, and the newer String.formatted() instance method.
Format specifiers start with % and describe the type and width of the value to insert. %s for String, %d for integer, %f for floating-point, %c for char, %n for newline.
Key Points
- •%s - String argument
- •%d - integer (int, long)
- •%f - floating-point; %.2f means 2 decimal places
- •%c - char
- •%n - platform-correct newline (prefer over \n in printf)
- •%10s - right-align in 10 characters wide; %-10s - left-align
- •%05d - pad with leading zeros: 007 → %03d gives "007"
- •String.format(pattern, args) returns a String; printf prints directly
Examples
Basic format specifiers
public class Main {
public static void main(String[] args) {
String name = "Alice";
int age = 28;
double gpa = 3.756;
System.out.printf("Name: %s%n", name);
System.out.printf("Age: %d%n", age);
System.out.printf("GPA: %.2f%n", gpa); // 3.76 (rounded)
// String.format builds and returns a String
String report = String.format("%-10s | %3d | %.1f", name, age, gpa);
System.out.println(report); // Alice | 28 | 3.8
}
}%.2f rounds to 2 decimal places. %-10s left-aligns in a 10-character field. %3d right-aligns in a 3-character field.
Formatting a table
public class Main {
public static void main(String[] args) {
System.out.printf("%-15s %8s %8s%n", "Product", "Price", "Qty");
System.out.println("-".repeat(33));
System.out.printf("%-15s %8.2f %8d%n", "Apple", 0.99, 150);
System.out.printf("%-15s %8.2f %8d%n", "Banana", 0.49, 300);
System.out.printf("%-15s %8.2f %8d%n", "Cherry", 2.99, 75);
}
}Using consistent field widths creates aligned table columns.
String.formatted() - modern Java
public class Main {
public static void main(String[] args) {
// Java 15+ - instance method on String
String msg = "Hello, %s! You have %d messages.".formatted("Bob", 5);
System.out.println(msg);
// Padding and zero-fill
System.out.printf("Code: %05d%n", 42); // Code: 00042
System.out.printf("Hex: %X%n", 255); // Hex: FF
System.out.printf("Sci: %e%n", 12345.678); // Sci: 1.234568e+04
}
}String.formatted() (Java 15+) is the object-oriented version of String.format().
Common Mistakes
- Mismatching format specifier and argument type: `printf("%d", 3.14)` throws IllegalFormatConversionException
- Using \n instead of %n in printf on Windows - %n produces the platform-correct line separator
- Too few arguments for the format string: `printf("%s %s", "only one")` throws MissingFormatArgumentException
Note
Use String.format() (or .formatted()) when building a String to store or return. Use printf() when printing directly. Both use the same format syntax.
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Loading editor…
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4