println() ,这个是方法名不难理解,但它是那个类的静态方法呢?
这个问题一会再来解答。
通过查API,发现System是个类,out是System的一个静态成员(public static final PrintStream out)
因此,我们可以知道out其实是PrintStream这个类的一个引用,其实System.out.println()这句语句正真调用了PrintStream的println()这个静态方法。
我有查看了System的源码,有这样与out相关的语句。
public final static PrintStream out = nullPrintStream();
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
public static void setOut(PrintStream out) {
checkIO();
setOut0(out);
}
private static native void setOut0(PrintStream out);
这里有一点我不太懂,为什么为out设置PrintStream的引用调用了C或C++呢?