加载系统类时,<clinit>
方法实例化 in
, out
和 err
PrintStream
变量到 null
使用 nullPrintStream()
方法:
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
我明白为什么会这样,为什么在加载过程中不能实例化变量,但我感到困惑的是那个方法的内容。
为什么要比较currentTimeMillis()
至 0
?在什么情况下该比较会返回 false
?
最佳答案
nullPrintStream() 方法的 Javadoc 给出了一个线索:
The compiler, however, cannot be permitted to inline access to them, since they are later set to more sensible values by initializeSystemClass().
我猜这是一个编码技巧,目的是防止编译器内联一个简单的“return null”实现。
currentTimeMillis() 永远不会小于 0。但编译器不够聪明,无法知道这一点,因此保留条件语句不变。
关于jvm - 为什么 java/lang/System 中的 nullPrintStream() 函数将 currentTimeMillis() 与零进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914685/