Results 1 to 11 of 11
 06212011, 12:33 AM #1Senior Member
 Join Date
 Nov 2010
 Posts
 167
 Rep Power
 6
system.out.printf versus system.out.format
Hello,
I am working on Numbers
and string manipulations and I don't understand why for the first half of the Math class, they use printf then format for the trigonometric methods. Is there an advantage to doing one versus the other?
Thank you!Last edited by bigsonny; 06212011 at 12:40 AM.
 06212011, 12:41 AM #2
As far as I am aware format and printf do the same thing. Perhaps it depends upon who wrote the methods in the Math class.
 06212011, 12:41 AM #3Senior Member
 Join Date
 Nov 2010
 Posts
 167
 Rep Power
 6
A sample code would help:
From: Beyond Basic Arithmetic (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
printf:
Java Code:public class ExponentialDemo { public static void main(String[] args) { double x = 11.635; double y = 2.76; System.out.printf("The value of e is %.4f%n", Math.E); System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x)); System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x)); System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y)); System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x)); } }
Java Code:public class TrigonometricDemo { public static void main(String[] args) { double degrees = 45.0; double radians = Math.toRadians(degrees); System.out.format("The value of pi is %.4f%n", Math.PI); System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians)); System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians)); System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians)); System.out.format("The arcsine of %.4f is %.4f degrees %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians)))); System.out.format("The arccosine of %.4f is %.4f degrees %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians)))); System.out.format("The arctangent of %.4f is %.4f degrees %n", Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians)))); } }
 06212011, 12:42 AM #4Senior Member
 Join Date
 Nov 2010
 Posts
 167
 Rep Power
 6
 06212011, 12:56 AM #5
What do you mean by "everything"? Are you asking are there methods that do the same thing throughout the API? Yes and no. There are a few methods that do the same or similar things but it is by no means prevalent. Why is this the case? I have no idea. I didn't work for Sun when they developed Java.
 06212011, 12:59 AM #6Senior Member
 Join Date
 Nov 2010
 Posts
 167
 Rep Power
 6
Sorry, I meant is it the case that format and printf do the same thing in every instance or are there differences that you are of that justifies their respective existence?
 06212011, 01:02 AM #7
From the Java API
An invocation of this method of the form out.printf(l, format, args) behaves in exactly the same way as the invocation
out.format(l, format, args)
 06212011, 01:21 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
The println() and format() methods of PrintStream/Writer do differ in what they return. format() returns the stream, so that you can chain method calls together. In this regard the PrintStream/Writer format() allows the same style of code as the Formatter version.
 06212011, 02:15 PM #9Senior Member
 Join Date
 Nov 2010
 Posts
 167
 Rep Power
 6
Got it. Thank you very much.
Riffard
 06212011, 02:23 PM #10
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,322
 Blog Entries
 7
 Rep Power
 25
Here's the source to 'prove' the identical behaviour; (I ripped this from the src.zip file):
Java Code:public PrintStream printf(String format, Object ... args) { return format(format, args);
JosThe only person who got everything done by Friday was Robinson Crusoe.
 06212011, 10:40 PM #11Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,712
 Rep Power
 14
Similar Threads

is System.out.print same as System.out.println ?
By usuf in forum New To JavaReplies: 8Last Post: 06212011, 02:21 PM 
System.out.println versus return: Perspective?
By bigsonny in forum New To JavaReplies: 6Last Post: 06192011, 08:42 PM 
Convert Cartesian coordinate system into java coordinate system?
By 123 in forum Java 2DReplies: 3Last Post: 02072010, 09:34 PM 
adding printf to system.in file
By bettyhagan in forum New To JavaReplies: 2Last Post: 12212009, 12:11 PM 
Default/System Date Format Pattern?
By Gajesh Tripathi in forum AWT / SwingReplies: 1Last Post: 10052008, 07:34 AM
Bookmarks