4

Is there any easier way of understanding how the numerous ways of formatting in java are related? I get confused of the following:

System.out.printf()
System.out.format()
String.format()
System.console.format()
new Formatter(new StringBuffer("Test")).format();
DecimalFormat.format(value);
NumberFormat.format(value);

Are the above classes/methods related in anyway? What is the best way to understand the differences and which to use in which situation?

As an example, System.out.printf, System.out.format and String.format all use the same syntax and format flags. I cant see what the difference is in all three of them.

Thanks

ziggy
  • 15,677
  • 67
  • 194
  • 287
  • 1
    They're just convenience methods wrapping up `Formatter` functionality. (Not sure how `NumberFormat` and its subclasses tie in to that, i.e., which way the relationship flows.) – Dave Newton Dec 31 '11 at 16:51
  • @Dave But still a good question why we do have `PrintStream.format` AND `PrintStream.printf` - afaik they were added in the same release and have the same functionality. The jdocs for printf even state: `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)`. Strange dublication really – Voo Dec 31 '11 at 17:12
  • @Voo I'd guess that someone added all the `printf` stuff to keep the `format` functionality more in line with `print`/`println` etc., but it's just a guess. Most APIs have some amount of duplication, for a variety of reasons--allow different styles, communicate intent differently, etc. – Dave Newton Dec 31 '11 at 17:17
  • 1
    @Dave Yeah I assume there's no deep mysterious explanation there, so it's just a matter of opinion. Personally I try to avoid exactly these things in my APIs though - to cite the Zen of Python `There should be one-- and preferably only one --obvious way to do it.`. At least the javadoc clearly states that they're the same so nobody will be wondering "where's the difference?" (well at least nobody that read the docs) – Voo Dec 31 '11 at 17:26
  • Similar to http://stackoverflow.com/questions/3080112/difference-between-system-out-printf-and-string-format – Gray Jan 02 '12 at 15:40

1 Answers1

4

I would consider downloading the javadocs and source jars for your corresponding Java version because all of your questions can easily be answered by looking at the source and docs.

System.out.printf(formatString, args)

System.out is a PrintStream. PrintStream.printf(formatString, args) is actually a convenience method call to PrintStream.format(formatString, args);.

System.out.format(formatString, args)

This is a call to PrintStream.format(formatString, args) which uses a Formatter to format the results and append them to the PrintStream.

String.format(formatString, args)

This method also uses a Formatter and returns a new string with the formatted results of the format string and args.

System.console().format(formatString, args)

System.console() is a Console. Console.format(format, args) uses a Formatter to display a formatted string to the console.

new Formatter(new StringBuffer("Test")).format(formatString, args);

This creates an instance of a Formatter using the string buffer passed in. If you use this call then you will have to use the out() method to get the Appendable written to by the Formatter. Instead you might want to do something like:

StringBuffer sb = new StringBuffer("Test");
new Formatter(sb).format(formatString, args);
// now do something with sb.toString()

Lastly:

DecimalFormat.format(value);
NumberFormat.format(value);

These are two concreate formatters for numbers that do not use the Formatter class. DecimalFormat and NumerFormat both have a format method which takes a double or Number and returns them formatted as a string according to the definition of those classes. As far as I can tell, the Formatter does not use them.

Hope this helps.

Community
  • 1
  • 1
Gray
  • 115,027
  • 24
  • 293
  • 354