Why there is no null pointer exception? [duplicate]

我们两清 提交于 2019-12-25 19:05:23

问题


In the documentation code of java "out" is an object of PrintStream class which is initialized in the System class of package lang.This object "out" is initialized to null.So why does'nt the code throws a null pointer exception whenever the line

System.out.println(...); is used


回答1:


It is the field declaration.

Look at the initializeSystemClass() static method :

setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")))

The out is well initialized at a time by the VM.

The method is commented with this :

/**
 * Initialize the system class.  Called after thread initialization.
 */



回答2:


The .out variable is a static variable of System. It's the default outputStream and not null.

Java Documentation: This stream is already open and ready to accept output data.



来源:https://stackoverflow.com/questions/43606406/why-there-is-no-null-pointer-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!