Why toString() on an Object instance (which is null) is not throwing NPE?

前端 未结 4 633
北荒
北荒 2021-01-04 05:41

Consider below one :

Object nothingToHold = null;

System.out.println(nothingToHold);  //  Safely prints \'null\'

Here, Sysout must be expe

4条回答
  •  抹茶落季
    2021-01-04 06:39

    Here is documentation for println()

    Prints a string followed by a newline. The string is converted to an array of bytes using the encoding chosen during the construction of this stream. The bytes are then written to the target stream with write(int).

    If an I/O error occurs, this stream's error state is set to true.

    NULL can convert to bytes.

提交回复
热议问题