Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By pbrockway2

Thread: system.out.printf versus system.out.format

  1. #1
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default 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; 06-21-2011 at 12:40 AM.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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));
        }
    }
    format:
    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))));
         
        }
    }

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    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.
    Oh sorry, I had not realized that you replied. Thanks. This seems redundant though. Is that the case for everything or is it just in the case that I am encountering right now?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  6. #6
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    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?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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)

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.
    Fubarable likes this.

  9. #9
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default

    Got it. Thank you very much.

    Riffard

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,368
    Blog Entries
    7
    Rep Power
    20

    Default

    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);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Here's the source to 'prove' the identical behaviour
    So you can ignore my reply #8. You live and learn! I had always thought they differed in what was returned - I guess I was thinking of println().

Similar Threads

  1. Replies: 8
    Last Post: 06-21-2011, 02:21 PM
  2. System.out.println versus return: Perspective?
    By bigsonny in forum New To Java
    Replies: 6
    Last Post: 06-19-2011, 08:42 PM
  3. Replies: 3
    Last Post: 02-07-2010, 08:34 PM
  4. adding printf to system.in file
    By bettyhagan in forum New To Java
    Replies: 2
    Last Post: 12-21-2009, 11:11 AM
  5. Default/System Date Format Pattern?
    By Gajesh Tripathi in forum AWT / Swing
    Replies: 1
    Last Post: 10-05-2008, 07:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •