Java comparison with == of two strings is false? [duplicate]

The == operator compares the object references, not the value of the Strings.

To compare the values of Strings, use the String.equals method:

"231".equals(parts[0]);

This is true with any other object in Java -- when comparing values, always use the equals method rather than using the == operator.

The equals method is part of Object, and should be overridden by classes which will be compared in one way or another.


If the strings are not interned, then == checks reference identity. Use:

 "231".equals(parts[0]);

instead.