A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 浮出一个美 中级黑马   /  2013-12-29 19:25  /  1034 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 浮出一个美 于 2013-12-30 17:05 编辑

代码如下:
public static void main(String[] args) {
        InputStream in = System.in;
        System.out.println(in);
}
控制台输出内容:java.io.BufferedInputStream@1fb8ee3
查看System源代码如下:
public final static InputStream in = nullInputStream();

private static InputStream nullInputStream() throws NullPointerException {
        if (currentTimeMillis() > 0) {
            return null;
        }
        throw new NullPointerException();
    }
我的问题,nullInputStream()这个方法不是返回了一个null给静态变量in吗?
为什么返回的不是null?却还是一个装饰了inputSteam的缓冲区对象?

2 个回复

倒序浏览
我这个问题太2了?可我确实很迷惑,求解答。
回复 使用道具 举报
好吧,这是System类in字段的初始化过程,应该可以解开你的疑。

VM will invoke the initializeSystemClass method to complete the initialization for this class separated from clinit.
虚拟机会调用initializeSystemClass方法来完成此类的初始化

public final static InputStream in = null;//声明的in字段

public static void setIn(InputStream in) //对外提供的标准流设置方法
{
        checkIO();
        setIn0(in);
}

private static native void setIn0(InputStream in);

private static void initializeSystemClass() //用于设置标准流的方法
{
   FileInputStream fdIn = new FileInputStream(FileDescriptor.in);

   setIn0(new BufferedInputStream(fdIn));


至于你说的那个方法,我反射了所有方法和字段,没找到。
所有方法,不包括继承的。
registerNatives***arraycopy***getCallerClass***getSecurityManager***getProperty***getProperty***load***loadLibrary***mapLibraryName***nanoTime***checkIO***checkKey***clearProperty***console***currentTimeMillis***exit***gc***getProperties***getenv***getenv***identityHashCode***inheritedChannel***initProperties***initializeSystemClass***lineSeparator***runFinalization***runFinalizersOnExit***setErr***setErr0***setIn***setIn0***setJavaLangAccess***setOut***setOut0***setProperties***setProperty***setSecurityManager***setSecurityManager0***
所有共有方法,包括继承。
arraycopy---getSecurityManager---getProperty---getProperty---load---loadLibrary---mapLibraryName---nanoTime---clearProperty---console---currentTimeMillis---exit---gc---getProperties---getenv---getenv---identityHashCode---inheritedChannel---lineSeparator---runFinalization---runFinalizersOnExit---setErr---setIn---setOut---setProperties---setProperty---setSecurityManager---getClass---hashCode---equals---toString---notify---notifyAll---wait---wait---wait---
所有字段。
in out err cons props lineSeparator


回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马