-2
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..

4
  • We use "equals()" method to check equality for objects (String in this case), not "==". Commented May 19, 2016 at 3:49
  • @aakash you don't use equals() to check for null. It would never return false. Commented May 20, 2016 at 0:47
  • @shmosel I never said that you use equals() to check for null. 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 used String.valueOf(lo).equals("null"). And as this is already answered by you, congrats. Commented May 20, 2016 at 3:48
  • @Aakash I think OP thought String.valueOf(null) returns null. In other words, he wasn't attempting to compare strings altogether. Commented May 20, 2016 at 3:53

1 Answer 1

2

Read the Javadoc:

Returns:

if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

In other words, the result is the string "null", not the value null.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.