<分区>
什么时候像这样格式化字符串输出被认为是好的代码:
int count = 5;
double amount = 45.6;
System.out.printf("The count is %d and the amount is %45.6", count, amount);
使用 printf 语句,代码如下:
int count = 5;
double amount = 45.6;
System.out.print("The count is " + count + "and the amount is " + amount);
使用打印语句?
我已阅读JavaDocs其中指出 printf 是“使用指定格式字符串和参数将格式化字符串写入此输出流的便捷方法”。
但是它没有说明按照惯例我们应该在什么时候使用一个而不是另一个?
那么,什么时候使用 printf 是好的编码,什么时候不使用 printf 是好的编码?
感谢您的帮助。