The equals()
method checks the actual contents of the string, the ==
operator checks whether the references to the objects are equal. Note that string constants are usually "interned" (except the argument values as they are not compile-time constants) such that two constants with the same value can actually be compared with ==
, but it's better not to rely on that.
if (string1.equals(string2)) {
...
}
Taken from: Java String.equals versus == , with some modifications.