System.out.println('a'); 實際上調用是PrintStream的println(char c)方法;而println(char c)方法的源代碼為:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
可見Println調用了print(char c)方法,print(char c)方法的源代碼如下:
public void print(char c) {
write(String.valueOf(c));
}
可見調用的是write(String s)方法,write(String s)的代碼為:
private void write(String s) {
try {
synchronized (this) {
ensureOpen();
textOut.write(s);
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush && (s.indexOf('\n') >= 0))
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
當字符串中含有'\n'時會刷新out,此處的out是OutStream對象的實例。println(String s)最後調用newLine() 方法,newLine()的代碼如下:
private void newLine() {
try {
synchronized (this) {
ensureOpen();
textOut.newLine();
textOut.flushBuffer();
charOut.flushBuffer();
if (autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
newLine()會刷新out。
System.out.write(a); 調用的是PrintStream.write(int b)方法
write(int b) 的源代碼如下:
public void write(int b) {
try {
synchronized (this) {
ensureOpen();
out.write(b);
if ((b == '\n') && autoFlush)
out.flush();
}
}
catch (InterruptedIOException x) {
Thread.currentThread().interrupt();
}
catch (IOException x) {
trouble = true;
}
}
看過源代碼後應該明白兩者之間的差異了,println(String s)不但會刷新out,而且還會同時刷新textOut和charOut,而write(int b)只有當b == '\n'時才刷新out。這也是為什麽加了System.out.write('\n'); 後就能顯示出來了,問題就在於out沒有刷新。
樓主的第二個問題很好解釋,因為在print(String s)中,會刷新textOut和charOut。
textOut和charOut是什麽?看壹下PrintStream中的定義:
private BufferedWriter textOut;
private OutputStreamWriter charOut;
textOut和charOut在init(OutputStreamWriter osw)方法中初始化,init(OutputStreamWriter osw)的代碼如下:
private void init(OutputStreamWriter osw) {
this.charOut = osw;
this.textOut = new BufferedWriter(osw);
}
init()函數在構造函數中被調用
public PrintStream(OutputStream out, boolean autoFlush) {
this(autoFlush, out);
init(new OutputStreamWriter(this));
}
可見,textOut和charOut操作的輸出流和out是壹樣的,因此對textOut和charOut刷新同時刷新了out,因此print(String s)即便沒有'\n',也同樣會直接輸出出來。