0

Possible Duplicate:
Concatenating null strings in Java

Why is obj equals "null"?

public static void main(String[] args) {
 Object obj = null+"";
 System.out.println(obj.equals("null"));
}

java version "1.6.0_23" Java(TM) SE Runtime Environment (build 1.6.0_23-b05)

Community
  • 1
  • 1
kalman03
  • 1,800
  • 2
  • 11
  • 7

2 Answers2

3

String.equals() compares the contents of the Strings.

The objects are both strings and contain the same data.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

Try this:

System.out.println(obj);

and then print:

System.out.println(obj.equals("null"));

Can you see it ... The content you saved is 'null' and you are matching content 'null'

Dusean Singh
  • 1,456
  • 2
  • 15
  • 20