Synopsis: | Use equals() to compare String objects |
Language: | Java |
Severity Level: | 1 |
Category: | StringandStringBuffer |
Description: |
Use equals() to compare object references; avoid comparing them with ==. This is because equals() compares object values and == only object references. So if the contents of 2 different string objects is equal (say, both have value "hello"), == returns false.
class Foo { boolean bar(String a, String b) { return a == b; // might unexpectedly return false } } |