Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PrintWriter.format vs. printf

 
Brian Kuhn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any difference between PrintWriter.format and PrintWriter.printf? The printf api says, "An invocation of this method of the form out.printf(format, args) behaves in exactly the same way as the invocation out.format(format, args)." If this is the case, what's the point of having two methods that do the same thing?
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"...what's the point of having two methods that do the same thing?"

Convenience. System.out.printf() does not require the explicit creation of a Formatter.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
 
Brian Kuhn
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.printf() does not require the explicit creation of a Formatter.


I'm sometimes a little dense...can you spell this out for me? The two java.io.PrintWriter methods in question have the exact same signature:

public PrintWriter format(String format, Object... args)
public PrintWriter printf(String format, Object... args)

And according to the javadocs, they do the same thing. How is one more convienent than the other? I'm not seeing how either requires the creation of a Formatter. Perhaps you thought I was talking about Formatter's format method.

Thanks,
Brian
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, They wanted to add System.out.printf() to Java

the "out" in the System object is a PrintWriter. That is what they wanted, now the format() method was already in Java, and instead of removing it and breaking all the code written using the format method, then kept the format method for backwards compatibility. I do know they added the varagrs to the format method, but that will not break backwards compatibility.

To me, it looks like they both have the exact same results.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic