Is there a good reason to use “printf” instead of “print” in java?

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\n", 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.

Leave a Comment