javastringequalsstring-comparisonstring-interning

String comparison and String interning in Java


When should one compare Strings as objects and when should one use their equals method? To make sure, I always use equals, but that doesn't seem very efficient. In what situations can I be certain that string1 == string2 is a safe to use?


Solution

  • You should almost always use equals. You can be certain that string1 == string2 will work if:

    It really doesn't happen very often, in my experience.