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

前端 未结 4 674
北荒
北荒 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:35

        Object nothingToHold = null;
       System.out.println(nothingToHold != null ? nothingToHold : "null String or any thing else");
    

    This will display output if the nothingToHold(Object) not equals to null, otherwise it prints the message as "null String or any thing else"

提交回复
热议问题