In Java, console.format(fmt, args...) builds text from a format string and a list of values, writes it to the terminal, and flushes it. Specifiers such as %s (text), %d (whole number), %.2f (decimal with two places), and %n (line break) mark where each value goes.
It follows the same rules as String.format(), but prints the result instead of returning it. It returns the Console itself, so several calls can be chained. printf() is an exact alias.
An instance method of the java.io.Console class, available since Java 6. You never create a Console yourself: get it with Console console = System.console(); after import java.io.Console;, and check it for null before calling console.format(...).
Locale assumption.
Console formatting uses the default locale (Locale.getDefault(Locale.Category.FORMAT)). The outputs on this page assume an English (US) default locale. In a German or French locale, %.2f prints a comma as the decimal separator, for example 7,00.
Syntax
Pass a format string, then one value for each specifier:
Two decimals with grouping, e.g. 1,234.50 in an English locale.
console.format("%n")
Prints the platform line separator.
Available since Java 6. Recent JDKs (Java 23 and later) also offer an overload that takes a Locale first. On older versions, when you need a fixed locale, build the text with String.format(Locale.US, ...) and print it with %s.
Library declarations
Java
// java.io.Console
public Console format(String fmt, Object... args)
public Console printf(String format, Object... args) // same behaviour
Parameters
Parameter
Description
fmt
Format string: fixed text plus specifiers such as %s, %d, %.2f, and %n. Must not be null.
args
Values for the specifiers, in order. Extra values are ignored; missing ones cause an exception.
Return Value
Return Value
Description
Console
The same console object, so you can chain: console.format(...).format(...).
Use widths and the - flag to line up item names, quantities, and amounts in columns.
Java
import java.io.Console;
public class ConsoleFormatReceipt {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
String[] items = {"Coffee", "Bagel", "Juice"};
int[] qty = {2, 1, 3};
double[] price = {3.5, 2.25, 4.0};
double total = 0;
console.format("%-8s %3s %8s%n", "Item", "Qty", "Amount");
for (int i = 0; i < items.length; i++) {
double amount = qty[i] * price[i];
total += amount;
console.format("%-8s %3d %8.2f%n", items[i], qty[i], amount);
}
console.format("%-8s %3s %8.2f%n", "Total", "", total);
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleFormatReceipt. Assumes an English (US) default locale, because format() and printf() use the default locale.
Item Qty Amount
Coffee 2 7.00
Bagel 1 2.25
Juice 3 12.00
Total 21.25
How It Works
Header with the same widths. The heading uses %-8s %3s %8s, matching the row pattern, so "Item", "Qty", and "Amount" sit exactly above their columns.
Left versus right.%-8s pads names on the right (text reads left to right), while %3d and %8.2f pad numbers on the left so their last digits line up.
Rounded money. 2 x 3.5 = 7.0, 1 x 2.25 = 2.25 and 3 x 4.0 = 12.0; %8.2f always shows two decimals, giving 7.00, 2.25, and 12.00.
Empty middle cell. The total row passes "" to %3s, which prints three spaces, so 21.25 still lands in the amount column.
Example 2 — Chain format() Calls Using the Returned Console
format() returns the console it was called on, so you can call it again on the result.
Java
import java.io.Console;
public class ConsoleFormatChain {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
Console same = console.format("Step %d of %d: %s%n", 1, 3, "download")
.format("Step %d of %d: %s%n", 2, 3, "unpack");
console.format("Step %d of %d: %s%n", 3, 3, "install");
console.format("Same object returned? %b%n", same == console);
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleFormatChain. The last part is a run where System.console() is null (IDE, redirected input).
Step 1 of 3: download
Step 2 of 3: unpack
Step 3 of 3: install
Same object returned? true
--- When System.console() returns null ---
No console available. Run this program from a terminal.
How It Works
Null guard first. In a terminal the guard is skipped; without a console, the program prints the fallback line and returns before any format() call could throw NullPointerException.
Call on the result. The first format() prints step 1 and returns the console, and the second .format(...) runs on that returned object to print step 2.
Three values per line. Each call fills %d, %d, and %s from left to right with the step number, the total, and the task name.
Identity check.%b prints the boolean same == console, which is true: chaining never creates a new object.
Example 3 — Default Locale vs a Fixed Locale
Console.format() always uses the default locale. To force a style, format the text with String.format(Locale, ...) first.
Java
import java.io.Console;
import java.util.Locale;
public class ConsoleFormatLocale {
public static void main(String[] args) {
Console console = System.console();
if (console == null) {
System.out.println("No console available. Run this program from a terminal.");
return;
}
double distance = 1234.5;
console.format("Default locale: %,.2f km%n", distance);
console.format("%s%n", String.format(Locale.GERMANY, "German style: %,.2f km", distance));
console.format("%s%n", String.format(Locale.US, "US style: %,.2f km", distance));
}
}
Terminal output
Run in a real terminal (Command Prompt, PowerShell, or macOS/Linux) with java ConsoleFormatLocale. Assumes an English (US) default locale, because format() and printf() use the default locale.
Default locale: 1,234.50 km
German style: 1.234,50 km
US style: 1,234.50 km
How It Works
Grouping flag. The , in %,.2f asks for a thousands separator, and .2 keeps two decimals, so 1234.5 becomes 1,234.50 with the assumed English default locale.
German rules.String.format(Locale.GERMANY, ...) swaps the symbols: a dot groups thousands and a comma marks decimals, giving 1.234,50.
Print the finished text. The already-formatted string is passed to console.format("%s%n", ...), so the console does no number formatting of its own and the chosen locale survives.
Predictable output. The Locale.US line looks the same on every computer, which is why fixed locales are used for logs and files.
Frequently Asked Questions
It formats its arguments according to a format string, writes the result to the console, and flushes the output.
There is none. printf is documented as a convenience method that behaves exactly like format with the same arguments.
The same Console object it was called on, which lets you chain calls such as console.format("a%n").format("b%n").
The default format locale of the JVM. To force a locale on Java 8, build the text with String.format(Locale.US, ...) and print it with %s.
A missing value or a wrong type throws an IllegalFormatException subclass, such as MissingFormatArgumentException or IllegalFormatConversionException. Extra values are ignored.