This might be a very basic question, but I still don't know the answer.
String abc = null;
System.out.println(abc);
Why does System.out.println
print "null" and does not throw NullPointerException
?
It's behaving as it's documented to. PrintStream.println(String)
is documented as:
Prints a String and then terminate the line. This method behaves as though it invokes
print(String)
and thenprintln()
.
PrintStream.print(String)
is documented as:
Prints a string. If the argument is
null
then the string"null"
is printed. Otherwise, the string's characters are converted into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of thewrite(int)
method.
When in doubt, read the documentation :)
See more on this question at Stackoverflow