加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

为什么java / lang / System中的nullPrintStream()函数将current

发布时间:2020-12-15 00:21:48 所属栏目:Java 来源:网络整理
导读:加载System类时, clinit方法使用nullPrintStream()方法实例化in,out和err PrintStream变量为null: private static PrintStream nullPrintStream() throws NullPointerException { if (currentTimeMillis() 0) { return null; } throw new NullPointerExcept
加载System类时,< clinit>方法使用nullPrintStream()方法实例化in,out和err PrintStream变量为null:
private static PrintStream nullPrintStream() throws NullPointerException {
    if (currentTimeMillis() > 0) {
        return null;
    }
    throw new NullPointerException();
}

我明白为什么会这样,为什么在加载过程中不能实例化变量,但是我所困惑的就是该方法的内容.

为什么将currentTimeMillis()与0进行比较?在什么情况下,比较会返回假?

解决方法

nullPrintStream()方法的Javadoc提供了一个线索:

The compiler,however,cannot be permitted to
inline access to them,since they are later set to more sensible values
by initializeSystemClass().

这是一个编码黑客,我猜,为了防止编译器内联一个简单的“返回null”实现.

currentTimeMillis()永远不会小于0.但是编译器不够聪明才能知道,因此保留条件语句.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读