0

Possible Duplicates:
String comparison and String interning in Java
What is the difference between .Equals and ==

Just a simple question about comparing strings. Why should i be using string.equals(string2) and not string==string2 ? Thank you

Community
  • 1
  • 1
Giannis
  • 5,286
  • 15
  • 58
  • 113
  • 1
    A couple of seconds of research on a search engine would really go a long way here. See also the 5-10 exact duplicates listed in the "Related" links section to the right. – Mark Peters Jun 12 '11 at 21:02
  • It turns out String comparison and String interning in Java is what i was looking for . Answers on what i didnt understand are there. Didnt see it before. Thanks – Giannis Jun 12 '11 at 21:09
  • String comparison and String interning in Java seems to have the answers i was looking . Thanks for pointing there. – Giannis Jun 12 '11 at 21:12
  • This has nothing to do with string interning. String interning just makes it so that `x == y` "works" if both `x` an `y` refer to the same interned string (are the *same* object). That is -- don't rely on it. Use `equals` for a string value equality test. –  Jun 12 '11 at 21:41

3 Answers3

1

equals tests if the strings' content is the same; == tests if both are the same object.

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • I remember from introduction to programming talking about reference.Is that what == checks ? if both strings reference to same object ? – Giannis Jun 12 '11 at 21:02
  • Yes, exactly. == is true if both references point to the same object. – Tony the Pony Jun 12 '11 at 21:02
  • What i dont understand is why i have never got into wrong outcome when i was using == to compare strings instead of .equals. – Giannis Jun 12 '11 at 21:06
  • Because of *string interning*, two string references with the same content are often also the same object, but you cannot rely on this... you should always use `equals` for string comparisons. – Tony the Pony Jun 12 '11 at 21:10
  • 1
    AFAICT, Java maps identical strings to identical object references. If your strings were dynamically generated, though, you might get problems. – Blender Jun 12 '11 at 21:11
  • Yes i do for a year now ... for some reason i question my self why i did that today . Thanks for response . – Giannis Jun 12 '11 at 21:11
  • @Belnder: I think that's right. If you read the string `textarea` from a file (i.e. dynamically generated), I don't believe the JVM will automatically match it to an existing string reference with the same content (Though you can force this behavior by calling `String.intern`) – Tony the Pony Jun 12 '11 at 21:17
0

In case you have two different String objects that have the same value.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
0

string==string2 is physical comparison and compares the references to objects. equals is logical comparison and the equality can be defined in equals() method which objects inherit this from Object (Parent of all types)

Erhan Bagdemir
  • 5,231
  • 6
  • 34
  • 40