Consider below one :
Object nothingToHold = null;
System.out.println(nothingToHold); // Safely prints \'null\'
Here, Sysout must be expe
PrintWriter
's println(Object)
(which is the method called when you write System.out.println(nothingToHold)
) calls String.valueOf(x)
as explained in the Javadoc:
/**
* Prints an Object and then terminates the line. This method calls
* at first String.valueOf(x) to get the printed object's string value,
* then behaves as
* though it invokes {@link #print(String)}
and then
* {@link #println()}
.
*
* @param x The Object
to be printed.
*/
public void println(Object x)
String.valueOf(Object)
converts the null to "null":
/**
* Returns the string representation of the Object
argument.
*
* @param obj an Object
.
* @return if the argument is null
, then a string equal to
* "null"
; otherwise, the value of
* obj.toString()
is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj)