Java String comparison FAQ: Why doesn't == work when comparing two String objects?

The == operator doesn't work when comparing two Java String objects, even if the Strings store the same content, because the == operator compares the two object references to each other. Since they aren't the same reference, they aren't equal.

In short, when comparing two Java String objects, compare them like this:

if (s1.equals(s2))
{
  // your code ...
}

and specifically do not compare two String objects like this:

// this is wrong
if (s1 == s2)
{
  // your code ...
}