On execution of following line :
System.out.println(null);
the result comes out to be null printed on console.
Why does that happen?
Telling from the sources of OpenJDK 1.6.0_22:
PrintStream:
public void println(Object x) {
String s = String.valueOf(x);
synchronized (this) {
print(s);
newLine();
}
}
String:
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Actually, at least in java version 1.8.0, System.out.println(null);
should not print null
. You would get an error saying something like:
reference to println is ambiguous, both method println(char[]) in PrintStream and method println(String) in PrintStream match.
You would have to cast as follows: System.out.println((String)null);
See coderanch post here.
I suppose you could also do System.out.println(null+"");
to accomplish same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With