CKTIS / Java
PrintStream.printf — formatted output
PrintStream.printf writes data according to a format string, for example to System.out. It controls how numbers and text are presented and allows an explicit locale.
Version: Java SE 21; repository runtime, production runtime not attested
Syntax
public PrintStream printf(String format, Object... args)
public PrintStream printf(Locale l, String format, Object... args)PrintStream.printf writes formatted data to a stream rather than returning text. It returns the same PrintStream, allowing chained calls. Both overloads behave like their corresponding format calls.
printf(String format, Object... args) uses the default settings for Locale.Category.FORMAT. printf(Locale l, String format, Object... args) applies the supplied locale; passing null as l disables localization. Extra arguments are ignored.
%s formats an argument's string representation, %d formats an integer in decimal, and %.2f formats a floating-point number with two digits after the decimal separator. %% writes %, and %n writes the platform-specific line separator; neither consumes an argument. printf does not automatically append a line ending.
An invalid format, incompatible argument type, or missing argument causes IllegalFormatException; a null format causes NullPointerException. Do not pass untrusted text as the format: use the fixed format "%s" and pass the text as an argument.
Examples
import java.util.Locale;
public class Main {
public static void main(String[] args) {
System.out.printf(Locale.US, "%s: %d | %.2f | 100%%%n", "Result", 3, 12.5);
}
}Result
Result: 3 | 12.50 | 100%
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.