*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PrintWriter.format vs. printf  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "PrintWriter.format vs. printf  " Watch "PrintWriter.format vs. printf  " New topic
Author

PrintWriter.format vs. printf

Brian Kuhn
Greenhorn

Joined: Nov 19, 2003
Posts: 7
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

Joined: Oct 23, 2004
Posts: 151
"...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

Joined: Feb 05, 2001
Posts: 17249
    
    6

Hey Brian.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Brian Kuhn
Greenhorn

Joined: Nov 19, 2003
Posts: 7
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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PrintWriter.format vs. printf
 
Similar Threads
printf() ??
java.Util.Formatter
Easy Queries
Not sure what's on the exam
System.out.printf("%1$c",65536);