8

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

Bhargav Modi
  • 2,605
  • 3
  • 29
  • 49
  • `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? – MadProgrammer Feb 27 '15 at 06:10
  • @MadProgrammer than what is the use of it if we have `format` and `printf` the same ? – Bhargav Modi Feb 27 '15 at 06:12
  • 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 process – MadProgrammer Feb 27 '15 at 06:15
  • @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. – Bhargav Modi Feb 27 '15 at 06:20
  • @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 :) – shikjohari Feb 27 '15 at 06:26
  • 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`. – osechet Feb 27 '15 at 08:04

1 Answers1

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.

Ryan Shukis
  • 345
  • 1
  • 10