In this section, you'll learn how to use printf to control exactly how your output looks — decimal places, spacing, alignment, and more. By the end, you'll be able to format strings, numbers, and percentages like a pro.
What is printf?
printf stands for "print formatted." Unlike a regular print statement that just dumps text and numbers as-is, printf gives you control over how that data is displayed. Think of it as the difference between a plain T-shirt and a tailored outfit — same content, much better presentation.
The Basic Syntax
Every printf statement follows this structure:
System.out.printf("format-string", arguments);
- format-string — defines how the output should look, combining plain text with placeholders for your data
- arguments — the actual values you want inserted into those placeholders
Let's put this into practice with a few examples.
Example 1: Formatting a String
Start with something simple — printing a name inside a sentence:
String name = "Alice";
System.out.printf("Hello, %s! Welcome to our program.\n", name);
Here, %s acts as a placeholder for a string value. Run this code, and you'll see:
Hello, Alice! Welcome to our program.
Example 2: Formatting Numbers
Next, let's format a decimal number — useful when working with prices or measurements:
double price = 19.99;
System.out.printf("The price of the item is $%.2f.\n", price);
%.2f tells Java to round the number to two decimal places. The output:
The price of the item is $19.99.
This is especially important when working with currency, where extra or missing decimals can cause real problems.
Example 3: Aligning Text
Now try aligning text and numbers so they line up cleanly — useful for tables or lists:
String item = "Book";
int quantity = 5;
System.out.printf("%-10s %5d\n", item, quantity);
%-10sleft-aligns the string within a 10-character space%5dright-aligns the integer within a 5-character space
Output:
Book 5
Notice how everything stays neatly aligned — this becomes essential once you're printing multiple rows of data.
Example 4: Displaying Percentages
To display a value as a percentage, use %f combined with a literal %%:
double score = 0.856;
System.out.printf("Your score is: %.1f%%\n", score * 100);
%.1f%% rounds to one decimal place and appends a percent sign. Output:
Your score is: 85.6%
Try this the next time you need to display grades, statistics, or performance metrics.
Example 5: Combining Multiple Data Types
Finally, let's combine several data types in a single printf statement:
String name = "Bob";
int age = 30;
double height = 5.9;
System.out.printf("%s is %d years old and %.1f feet tall.\n", name, age, height);
Output:
Bob is 30 years old and 5.9 feet tall.
As you can see, printf lets you mix strings, integers, and decimals in one clean, readable statement.
Recap
You've now covered the core building blocks of printf:
| Format Specifier | Used For |
|---|---|
%s |
Strings |
%d |
Integers |
%.2f |
Decimals (with fixed precision) |
%-10s / %5d |
Alignment (left/right) |
%% |
Literal percent sign |
Try combining these in your own programs — once you get comfortable with formatting, your output will look far more polished and professional.
.png)