difference between printf and println in java? [duplicate]
Asked Answered
T

1

8

Just came to know that java does has a method named printf, then what is the difference between printf & println?

Troika answered 27/2, 2015 at 6:8 Comment(6)
printf is like the C printf function, "print format", the other is a more (or less) updated version, which doesn't require a format and value list...What does the JavaDoc's tell you?Picrite
@Picrite than what is the use of it if we have format and printf the same ?Troika
Because for many, printf is confusing and be difficult to use if you've not used it before. You can use println("Say " + word) or printf("Say %s%n", word), which one is generally more easier to read? C doesn't have a String in the sense that Java does, so allowing println simplifies the processPicrite
@shikjohari kindly read the question properly as it's about difference and not about which one is better and I had gone through your mentioned before posting this question.Troika
@BhargavModi Well if you go through the answer of that question, you ll understand why I said this as a dupclicate. Read the asnwers also properly :)Sheronsherourd
The difference is not very important since both should be avoided. It's better to use a logger to output your text. And if you need your logged text to be formatted, use String.format.Hiramhirasuna
I
9

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.

Iso answered 27/2, 2015 at 6:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.