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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 王贺 中级黑马   /  2013-11-26 21:56  /  1308 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

public static void main(String[] args) {
  // TODO Auto-generated method stub
  InputStream input = System.in;         
  System.out.println(input);
}
今天测试了一下System.in的输出
输出结果是java.io.BufferedInputStream@c17164
问一下是怎么初始化这个input的,为什么是输出上面的结果?

评分

参与人数 1黑马币 +5 收起 理由
枫儿 + 5 神马都是浮云

查看全部评分

4 个回复

倒序浏览
你这样写就等于是直接打印了input这个对象了,哥们是想将键盘输入的数据打印在控制台上吗?那应该用IO流的read()方法读取的,
  1. import java.io.*;
  2. class Demo
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                 InputStream input = System.in;
  7.                 byte[] buf = new byte[1024];
  8.                 int len = input.read(buf);
  9.                 //打印键盘录入的数据到控制台
  10.                 System.out.println(new String(buf,0,len));
  11.                 input.close();
  12.         }
  13. }
复制代码

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1 赞一个!

查看全部评分

回复 使用道具 举报
同求,技术大牛们,来看看啊
回复 使用道具 举报
哥们,你这个问题得去追究源码了。
System.in是一个static的InputStream类对象。源码中是这么写的
  1. public final static InputStream in = null;
  2. public static void setIn(InputStream in) {
  3.         checkIO();
  4.         setIn0(in);
  5. }
  6. setIn0(new BufferedInputStream(fdIn));
复制代码

所以实际上,in的是一个BufferedInputStream对象,最后通过println打印的时候会调用如下的方法
  1. public void println(Object x) {
  2.         String s = String.valueOf(x);
  3.         synchronized (this) {
  4.             print(s);
  5.             newLine();
  6.         }
  7. }

  8. public static String valueOf(Object obj) {
  9.         return (obj == null) ? "null" : obj.toString();
  10. }
  11. public String toString() {
  12.         return getClass().getName() + "@" + Integer.toHexString(hashCode());
  13. }
复制代码

所以,到最后会打印出“类名@哈希值”的这种形式。

明白了么?

评分

参与人数 1技术分 +1 收起 理由
简★零度 + 1

查看全部评分

回复 使用道具 举报
版主~~~求加分~~~{:soso_e106:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马