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.