Displaying a value in the console is easy, but professional programs often need more control over how that value appears. For example, you may want a price to show exactly two decimal places, align values into columns, or combine several values into a clean message. This is where formatted output becomes useful.
What Is Formatted Output?
Formatted output means controlling how data is displayed in the console instead of simply printing the raw value.
String name = "Bibhu";
double marks = 85.5678;
System.out.printf("Name: %s%n", name);
System.out.printf("Marks: %.2f%n", marks);
The output is:
Name: Bibhu Marks: 85.57
Notice that the original marks value contains more decimal places, but the formatted output displays only two. The program is controlling the presentation without changing the stored value.
Formatted output changes how information is displayed; it does not automatically change the original value stored in the variable.
Using printf()
Java provides the printf() method for formatted output. It is available through System.out.
System.out.printf("Hello %s", "Java");
The %s is called a format specifier. It tells Java where and how a value should be inserted into the output.
Basic Format Specifiers
Different data types use different format specifiers.
| Specifier | Used For | Example |
|---|---|---|
| %s | String | printf("%s", name) |
| %d | Integer | printf("%d", age) |
| %f | Floating-point value | printf("%f", price) |
| %c | Character | printf("%c", grade) |
| %b | Boolean | printf("%b", passed) |
Formatting a String
The %s specifier is used when inserting a String into formatted output.
String name = "Bibhu";
System.out.printf("Student Name: %s%n", name);
Here, %s is replaced by the value stored in name.
Formatting an Integer
The %d specifier is commonly used for integer values.
int age = 25;
System.out.printf("Age: %d%n", age);
The output is:
Age: 25
Formatting Decimal Values
The %f specifier is used for floating-point values.
double price = 125.5;
System.out.printf("Price: %f%n", price);
By default, the output contains six digits after the decimal point.
Price: 125.500000
If you want to control the number of decimal places, place the required precision between the percent sign and the format specifier.
double price = 125.5678;
System.out.printf("Price: %.2f%n", price);
The output becomes:
Price: 125.57
Here, .2 means that two digits should be displayed after the decimal point.
Using %n for a New Line
Formatted output commonly uses %n to move to the next line.
System.out.printf("Hello%n");
System.out.printf("Java%n");
The output is:
Hello Java
Using %n makes the new-line requirement part of the formatting expression.
Using Multiple Values
One printf statement can contain multiple format specifiers and multiple values.
String name = "Bibhu";
int age = 25;
double marks = 85.5;
System.out.printf("Name: %s, Age: %d, Marks: %.1f%n",
name, age, marks);
The format specifiers are matched with the values in the same order. The first %s receives name, %d receives age, and %.1f receives marks.
Controlling Width
Formatted output can also reserve a minimum amount of space for a value. This is useful when creating simple console tables.
System.out.printf("%10s%n", "Java");
System.out.printf("%10s%n", "Python");
The number 10 specifies a minimum field width of ten characters. If the value is shorter, Java adds spaces to fill the field.
Creating Simple Console Columns
Width formatting becomes particularly useful when displaying related information in columns.
System.out.printf("%-15s %5s%n", "Name", "Age");
System.out.printf("%-15s %5d%n", "Bibhu", 25);
System.out.printf("%-15s %5d%n", "Rahul", 27);
The - flag left-aligns the value within the specified width. This makes the output easier to read when several records are displayed.
printf() vs println()
Both methods display information, but they are designed for different purposes.
| Method | Main Purpose | Formatting Control |
|---|---|---|
| print() | Simple output without automatic new line | Limited |
| println() | Simple output followed by a new line | Limited |
| printf() | Formatted output | High |
Real-World Example
Imagine a student result program. Simply printing the raw values may produce inconsistent decimal formatting.
String name = "Bibhu";
double percentage = 86.75642;
System.out.println("Student: " + name);
System.out.println("Percentage: " + percentage);
The percentage may contain more decimal places than we want to show. Formatted output gives us better control.
String name = "Bibhu";
double percentage = 86.75642;
System.out.printf("Student: %s%n", name);
System.out.printf("Percentage: %.2f%%%n", percentage);
The output becomes:
Student: Bibhu Percentage: 86.76%
The first % introduces the format specifier, while %% is used when you want to display an actual percent symbol.
Common Beginner Mistakes
One common mistake is using the wrong format specifier for the value.
int age = 25;
System.out.printf("%s%n", age);
The format specifier should match the type and intended formatting of the value.
int age = 25;
System.out.printf("%d%n", age);
Another common mistake is forgetting that a percent symbol used as normal text must be escaped.
double percentage = 85.5;
System.out.printf("%.1f%%%n", percentage);
The %% tells Java that an actual percent character should be displayed.
Think of printf() as a formatting instruction: the text describes the layout, the format specifiers identify where values belong, and the arguments provide those values.
Interview Insight
A common interview question is why printf() is useful when Java already provides print() and println(). The key answer is that printf() provides precise control over formatting, including decimal precision, field width, alignment, and multiple values in a single output statement.
Quick Revision
| Specifier | Purpose |
|---|---|
| %s | Formats a String. |
| %d | Formats an integer. |
| %f | Formats a floating-point value. |
| %.2f | Displays two digits after the decimal point. |
| %n | Creates a new line in formatted output. |
| %% | Displays an actual percent symbol. |
| %10s | Uses a minimum field width of ten characters. |
Formatted output gives your Java programs a more professional console presentation. Instead of simply displaying values, you can control precision, spacing, alignment, and structure. This becomes especially valuable when displaying reports, calculations, student results, prices, or multiple records in a readable format.
