When you use ==
on strings, only the references are compared. Thus, ==
is only guaranteed to return true
in situations like:
String s1 = "...";
String s2 = s1; // reference assignment!
Here, s1 == s2
. In all other situations, ==
may or may not return true
even if the two strings contain the same character sequence.
To compare the contents of the two strings, use equals()
:
if (s1.equals(s2)) {
...
}