TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
 
TIOBE - Java Coding Standard
Search

Rule:  Design33

Synopsis:Use equals() to compare object references
Language:Java
Severity Level:1
Category:Design


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
  }
}