Formatted output

The C-like Java 5 method System.out.printf() is used for formatted output. The method takes as arguments a format string and an optional list of variables or literals to output. The variables and literals are output according to the specifications in the format string. Here is the prototype for the original C function printf().

int printf(const char *format, arg_1, arg_2, arg_3, ... , arg_n);

The format string may consists both of characters and literals, and determines how printf() will format arg_1 - arg_n. Each of the arguments arg_1 - arg_n requires a format specifier, begins with the character % and has the following syntax:

%[-][+][0][x[.y]]conv

Here the terms put in brackets are optional. The meaning of these terms is as follows:

term meaning
- left justify the argument
+ print a number with its sign, + or -
0 pads a number with leading zeros
x minimum field width
.y number of decimal places

The conv is a character from the following list:

conv meaning
b boolean (true/false)
d integer
e exponential notation
f floating point number
o unsigned octal
s string of characters
x unsigned lower-case hexadecimal
X unsigned upper-case hexadecimal

The format string, or strings to be output, can contain special control sequences (escape sequences):

ECS sequence meaning
\' single quote
\" double quote
\\ backslash
\b backspace
\f form feed
\n new line
\r carriage return
\t horizontal tab

For more information please check printf docs in Java API.