我会考虑为您的相应Java版本下载javadocs和源代码jar,因为通过查看源代码和文档可以轻松回答所有问题.
System.out.printf(formatString, args)
System.out是一个PrintStream.PrintStream.printf(formatString,args)实际上是对PrintStream.format(formatString,args);的一个方便的方法调用.
System.out.format(formatString, args)
这是对PrintStream.format(formatString,args)的调用,它使用Formatter格式化结果并将它们附加到PrintStream.
String.format(formatString, args)
此方法还使用Formatter并返回带有格式化字符串和args的格式化结果的新字符串.
System.console().format(formatString, args)
System.console()是Console.Console.format(format,args)使用Formatter将格式化的字符串显示到控制台.
new Formatter(new StringBuffer("Test")).format(formatString, args);
这将使用传入的字符串缓冲区创建Formatter的实例.如果使用此调用,则必须使用out()方法来获取Formatter写入的Appendable.相反,你可能想做类似的事情:
StringBuffer sb = new StringBuffer("Test");
new Formatter(sb).format(formatString, args);
// now do something with sb.toString()
最后:
DecimalFormat.format(value);
NumberFormat.format(value);
这些是不使用Formatter类的数字的两个concreate格式化程序. DecimalFormat和NumerFormat都有一个格式方法,它采用double或Number,并根据这些类的定义将它们格式化为字符串.据我所知,Formatter不使用它们.
希望这可以帮助.