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
.
What is the difference between println and printf?
println
adds a new line at the end of the output, so you cant continue on that line and printf
supports format strings so you can pass arguments that are substituted into the string e.g.
public static void main(String[] args) {
System.out.printf("%s %s", "hello", "world");
System.out.printf(" - with something else on the same line");
}
will print
hello world - with something else on the same line
What is the benefit of using System.out.print over System.out.printf?
print
with string concatenation will be marginally more efficient than printf
. With print
, you the developer are determining at compilation time where the values need to be inserted into the string, but with printf
, you're asking the JRE to parse your format string and insert the values at runtime. Unless you're doing a large amount of printing, this is likely to be a negligible difference, so you should go with whatever is easiest to maintain.
What is the advantages of printf() (formatted print) over normal print()?
With printf()
you can do a lot of advanced formatting which print()
can't do
Check this out
difference between printf and println in java?
System.out.println();
is efficient for simply printing a line of text. If the line of text needs to be formatted (ex: alignment (left-justified, etc.), etc.), then System.out.printf();
would be used.
Check out this link for more information.
When to use a printf statement in Java?
Two methods are provided for the convenience of a coder and for different needs. If you need string formatting then go for printf method but if there is no formatting required simply use print method and void %d,%s, etc formatters.
If you want to control the precision & padding of floating point numbers then use printf otherwise go for print.
Which is preferred System.out.print() or System.out.printf()?
Your question actually demonstrates the preferred use cases: if you want to inject values into the text being printed (especially if you need specific formatting), then use printf()
; otherwise, use print()
or println()
, depending on whether you want to append a line break at the end.
As an aside, if you want to insert a platform-dependent line break when using printf()
, you can use the %n
escape sequence.
Is Java's printf() a bad practice?
No it is not true.
The only possible rationale I can think of is that printf
with a literal \n
and/or \n
in the format string is going to produce platform specific line breaks ... which is a portability issue. But the simple solution is to use %n
in the format string.
Of course, using printf
when no arguments need to be substituted into the format string is less than optimal. I wouldn't say that this makes it Bad Practice though.
Related Topics
Log4J Configuration via Jvm Argument(S)
How to Prevent Java.Lang.String.Split() from Creating a Leading Empty String
How to Convert Date in to Yyyy-Mm-Dd Format
This: Cannot Use This in Static Context
Compilation Error: Identifier Expected
How to Check If a Date Object Equals Yesterday
How to Read a File from a Certain Offset in Java
In What Situations Is the Copyonwritearraylist Suitable
What Determines Kafka Consumer Offset
Deciphering Variable Information While Debugging Java
Is It Better Practice to Use String.Format Over String Concatenation in Java
Jtable with Horizontal Scrollbar
Kill -3 to Get Java Thread Dump
How and Where to Use Static Modifier in Java