0

I know == compares references.

And equals compares content.

How come "a1" == "a1"; returns true?

It's a different instance if string after all. no?

Elad Benda2
  • 13,852
  • 29
  • 82
  • 157
  • 1
    The accepted answer covers this case. – Luiggi Mendoza Dec 24 '14 at 15:50
  • have a look at http://stackoverflow.com/questions/18636561/why-is-this-not-throwing-a-nullpointerexception/18644819#18644819 – Parth Soni Dec 24 '14 at 15:50
  • duplicate of: http://stackoverflow.com/questions/3052442/what-is-the-difference-between-text-and-new-stringtext-in-java – Joeblade Dec 24 '14 at 15:53
  • In case its not clear, Java interns String literals so in your code the two String literals are actually the same object. If you were to change it to 'new String("a1") = "a1";' then it would be false. – Charles Goodwin Dec 24 '14 at 15:53

0 Answers0