Originally posted by Ashok Pradhan:
Object o=null;
System.out.println(o); //prints null
My doubt is when we want to print an object then the toString() method of the object is called on that object.but here reference variable o is null,so when the toString() method is called on System.out.println(o.toString()); then it should throw NPE.
clarify my doubt why it prints null instead of NPE.
It's quite simple, the println() method checks for null, before calling the toString() method. If it is null, then it uses "null" as the
string to print.
When you call toString() directly, you didn't check for null, hence, it threw an NPE.
Henry