The printf
method of the PrintStream class provides string formatting similar to the printf
function in C.
The formatting for printf
uses the Formatter
class' formatting syntax.
The printf
method can be particularly useful when displaying multiple variables in one line which would be tedious using string concatenation:
int a = 10;
int b = 20;
// Tedious string concatenation.
System.out.println("a: " + a + " b: " + b);
// Output using string formatting.
System.out.printf("a: %d b: %d
", a, b);
Also, writting Java applications doesn't necessarily mean writing GUI applications, so when writing console applications, one would use print
, println
, printf
and other functions that will output to System.out
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…