初窥JAVA底层————System.out.println("Hello World");

今天从头开始看《Thinking In Java》,不愧是Java的圣经,感觉收获挺大,虽然有些地方仍然一头雾水,但是较之以前已经好了很多。以前使用Java只是流于表面,顶层的API的调用,而没有思考、查找底层是如何实现的,现在通过阅读《Thinking In Java》能够在更深的层次里面来看待Java。
今天就来看一下Java简单的输出语句 System.out.println("Hello world");
究竟是如何实现的。

**System**

首先Systemjava.long 包的类(java.long 包在一开始创建.class 文件时就已经加载完,所以不用手动加入。)通过查阅JDK 1.8的文档,对于System类的描述为:

The System class contains several useful class fields and methods. It cannot be instantiated. 
Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array.

翻译:System类包含几个非常有用的类域(in、out、err)和方法。System不能被实例化。
①System类所提供的一些工具有标 准输入流、标准输出流、和错误输出流。
②System类提供能得到在外部定义的属性和环境变量。
③System类是加载文件和库的类型。
④提供了快速复制一个数组中某一部分的公共方法。
然后outSystem的一个类变量,是PrintStream的静态对象。不需要提前声明、实例化。

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. 
``` **`out`** 
“标准”输出流。这个流早已经打开,并且准备去接收所要输出的数据。通常`out` 要和播放输出或者其他由环境或者使用者规定的输出目的地。
**`out`(`PrintStream`)的静态方法`println()`** **。**
`println()`方法有多个重载方法接受参数有`int、float、double、char[]、char、String、Object`等等 。println()方法先调用`print(object obj)`然后调用`println()` 

**然后看具体的实现:**
首先是将`String`类型的`Hello world`传入到`println(Object obj)` 方法中。




<div class="se-preview-section-delimiter"></div>

public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}



先是调用`String.valueOf(x)` 得到String类型的值s(`Object` 类型调用`toString()`方法),然后调用`print(s)`方法,然后再进入到`print(s)` 中,代码如下:





<div class="se-preview-section-delimiter"></div>

public void print(String s) {
if (s == null) {
s = “null”;
}
write(s);
}
“`
检查待打印的值是否为空,不为空继续打印,执行write

public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }

先是调用String.valueOf(x) 得到String类型的值s(Object 类型调用toString()方法),然后调用print(s)方法,然后再进入到print(s) 中,代码如下:

public void print(String s) {
        if (s == null) {
            s = "null";
        }
        write(s);
}

检查待打印的值是否为空,不为空继续打印,执行write(s) 方法。查看该方法的代码:

private void write(String s) {
        try {
            synchronized (this) {
                ensureOpen();
                textOut.write(s);
                textOut.flushBuffer();
                charOut.flushBuffer();
                if (autoFlush && (s.indexOf('\n') >= 0))
                    out.flush();
            }
        }
        catch (InterruptedIOException x) {
            Thread.currentThread().interrupt();
        }
        catch (IOException x) {
            trouble = true;
        }
    }

然后发现执行打印的代码是textOut.write(s) 代码。定位textOut后是BufferedWriter的一个对象。查阅JDK文档,查找BufferedWriter
BufferedWriter
查阅BufferedWriter 类的继承关系,继承自ObjectWriter类。Writer类的描述如下:

Abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.

翻译:
这个抽象类是为了向字符流中执行写入。writer的每个子类要实现的方法是write(char[] ,int ,int ),flush()close()。大多数的子类为了实现高效率,或者功能的可加性需要重载在这里定义的方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值