3 回答

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊
貌似跟緩存沒啥關(guān)系,println的源碼是這樣的:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
然后是printf的源碼:
public PrintStream printf(String format, Object ... args) {
return format(format, args);
}
在format里面就是單純的格式化操作沒啥了
public PrintStream format(String format, Object ... args) {
try {
synchronized (this) {
ensureOpen();
if ((formatter == null)
|| (formatter.locale() != Locale.getDefault()))
formatter = new Formatter((Appendable) this);
formatter.format(Locale.getDefault(), format, args);
}
} catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
} catch (IOException x) {
trouble = true;
}
return this;
}
至于你的那個(gè)問題你可以把代碼里打印輸出的那一句改為:
System.out.printf("Number is Prime: %d", number);
System.out.print("\r");
就可以了

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
Integer number = 55;
System.out.printf("Number " + number + " is Prime\n");
System.out.printf("Number %s is Prime", number);
換行是一個(gè)區(qū)別,但是最主要的區(qū)別是printf
可以格式化表達(dá)式,具體格式語(yǔ)法可參考java.util.Formatter
添加回答
舉報(bào)