今天终于会写System.out.println()了

最近在整理IO流的笔记时,发现了这样一个问题······

理解 System.out.println()

在Java.lang包下有一个System类,其中定义了一个类(静态)成员变量:
public final static PrintStream out = null;
由此可见,out只不过是PrintStream的一个对象。由于out在System类中是静态修饰的,所以可以直接通过类名来调用System.out。(System.in也是一样的道理,这里就不详细说了。)
现在,我们拿到了PrintStream类的一个对象System.out,通过对象调用方法(println)就更好理解了,所以就得到了这样的语句System.out.println

//一般不会有人这样写,只是便于理解。
	PrintStream Myout = System.out;
    Myout.println("Sivan");

空指针的out

这里out作为对象但是声明为null。但是为什么没有抛出NullPointerException?
因为在System类中有一个静态初始化块:

/* 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在加载类时,就是通过registerNatives()这个静态方法给out赋了初始值,至于具体的操作,可以参考这篇文章:https://zhidao.baidu.com/question/38660871.html。


由此可见,Java中万事万物皆对象。

最重要的来啦~~~
⭐码字不易,求个关注⭐
⭐点个收藏不迷路哦~⭐

  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Sivan_Xin

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值