System类属性值问题

zhangxm2015 2015-04-06 10:01:59
我们常用的System.out.println("--------");
System的源码:
 / /**
* The "standard" output stream. This stream is already
* open and ready to accept output data. Typically this stream
* corresponds to display output or another output destination
* specified by the host environment or user.
* 。。。。。。。。。。。

*/
public final static PrintStream out = nullPrintStream();


nullPrintStream源码:
private static PrintStream nullPrintStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}


我想问的是System.out的对象(PrintStream)值不是null吗?调用println方法为甚么没有抛空指针异常?
源码解释不是说这个流已经被打开了正准备接受数据吗?
难道是在引用System时,在静态代码块做的?
/* First thing---register the natives */
private static native void registerNatives();
static {
registerNatives();
}

...全文
146 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangxm2015 2015-04-07
  • 打赏
  • 举报
回复
引用 4 楼 bao110908 的回复:
不是在 registerNatives 方法中做的哦。你再仔细地看一下 System.java 的源代码:
    /* register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }
JVM 会调用这个类中的 initializeSystemClass 方法,其中使用 setOut 方法会重置 out 的值。之所以初始化为 nullPrintStream 指的是在 JVM 还没有调用 initializeSystemClass 方法之前,这个 out 是不可用的。
非常感谢!你提供的源码看了下,不过没发现有这样的注释啊?!! 我用4,6,7均没发现! 不过发现了这段:

/**
     * The following two methods exist because in, out, and err must be
     * initialized to null.  The compiler, however, cannot be permitted to
     * inline access to them, since they are later set to more sensible values
     * by initializeSystemClass().
     */
    private static InputStream nullInputStream() throws NullPointerException {
	if (currentTimeMillis() > 0) {
	    return null;
	}
	throw new NullPointerException();
    }

    private static PrintStream nullPrintStream() throws NullPointerException {
	if (currentTimeMillis() > 0) {
	    return null;
	}
	throw new NullPointerException();
    }
在nullInputStream方法上面,这里提供的意思和你的一样! 十分感谢!
  • 打赏
  • 举报
回复
不是在 registerNatives 方法中做的哦。你再仔细地看一下 System.java 的源代码:
    /* register the natives via the static initializer.
     *
     * VM will invoke the initializeSystemClass method to complete
     * the initialization for this class separated from clinit.
     * Note that to use properties set by the VM, see the constraints
     * described in the initializeSystemClass method.
     */
    private static native void registerNatives();
    static {
        registerNatives();
    }
JVM 会调用这个类中的 initializeSystemClass 方法,其中使用 setOut 方法会重置 out 的值。之所以初始化为 nullPrintStream 指的是在 JVM 还没有调用 initializeSystemClass 方法之前,这个 out 是不可用的。
TIMEST0RY 2015-04-06
  • 打赏
  • 举报
回复
Nullprintstream方法有什么用吗?
zhangxm2015 2015-04-06
  • 打赏
  • 举报
回复
引用 1 楼 u013761410 的回复:
其实我觉得你没必要纠结这个流,他是自己打开,自己关闭,根本不用我们管,你不用,他也开着,就当java帮你new了,多学其他吧,别纠结这个,就好像本地方法根本不用看
首先感谢回答,答主的意见接受了,但不太赞同。 不错这里的流具有自动管理机制,你确定是本地方法对System.out,in,error这样的流属性进行了维护,如果是这样也应该是在nullPrintStream 方法中进行维护吧?!
songjy3 2015-04-06
  • 打赏
  • 举报
回复
其实我觉得你没必要纠结这个流,他是自己打开,自己关闭,根本不用我们管,你不用,他也开着,就当java帮你new了,多学其他吧,别纠结这个,就好像本地方法根本不用看

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧