← Back to Core Java Course | Chapter 6: Strings | Lesson 4 of 5

Java String Formatting

String formatting lets you build tidy text from patterns with blanks, like filling in a template. You control decimals, padding, and dates.
Syntax
java
String.format("format %s %d", value1, value2);
System.out.printf("format %s%n", value);

Basic String.format

String.format() builds a formatted string using placeholders like %s for text, %d for integers, and %f for decimals, then hands you back the finished String to store, log, or pass along — it doesn't print anything itself.

Example: Basic String.format

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		String result = String.format("%s is %d years old", "Alice", 30);
		// Print a line to the console
		System.out.println(result);
	}
}

Printing directly with printf

System.out.printf() uses the exact same format-specifier syntax as String.format(), but writes the result straight to the console instead of returning a String, which saves a step when you just want to display formatted output immediately.

Example: Printing directly with printf

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print formatted text to the console
		System.out.printf("%s is %d years old%n", "Alice", 30);
	}
}

Formatting Decimal Places

The %f specifier formats floating-point values, and you can control precision with a modifier like %.2f to show exactly two decimal places — without it, Java defaults to six decimal places, which is rarely what you want for things like currency.

Example: Formatting Decimal Places

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare price and set it to 9.5
		double price = 9.5;
		// Print formatted text to the console
		System.out.printf("%.2f%n", price);
	}
}

Integer Padding and Zero Fill

Flags like %5d (minimum width, right-padded with spaces) or %05d (zero-padded) let you align columns of numbers so they print neatly, which matters when formatting tabular console output like a report or a scoreboard.

Example: Integer Padding and Zero Fill

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print formatted text to the console
		System.out.printf("%5d%n", 42);
		// Print formatted text to the console
		System.out.printf("%05d%n", 42);
	}
}

Formatting Date & Time Elements

The %t specifier family formats date and time values, combined with a conversion character such as Y for a four-digit year or m for a two-digit month — you need one %t group per component you want to display, since each only extracts a single piece of the date.

Example: Formatting Date & Time Elements

java
// Import java.util.Calendar so it can be used by its short name
import java.util.Calendar;
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Calendar cal = Calendar.getInstance();
		// Print formatted text to the console
		System.out.printf("%tY-%tm%n", cal, cal);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using %d for a decimal number, which throws IllegalFormatConversionException.
  2. Forgetting %n or \n when printing several lines with printf.
  3. Passing fewer arguments than format specifiers, which throws MissingFormatArgumentException.
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.