public static void main(String[] args) {
Long lo = null;
System.out.println(String.valueOf(lo) == null);
}
Why the above statement return false ? Can anyone please explain..
Read the Javadoc:
Returns:
if the argument is
null, then a string equal to"null"; otherwise, the value ofobj.toString()is returned.
In other words, the result is the string "null", not the value null.
equals()to check for null. It would never return false.equals()to check fornull. Please read my comment again. Certainly there were some gaps in my comment, which I thought could be easily filled by OP's understanding of Java.String.valueOf(lo)would return a string "null", which is not null. So, if OP wants to check that, he should have usedString.valueOf(lo).equals("null"). And as this is already answered by you, congrats.String.valueOf(null)returnsnull. In other words, he wasn't attempting to compare strings altogether.