Comparison of Object References Instead of Object Contents |
Weakness ID: 595 (Weakness Base) | Status: Incomplete |
Description Summary
Example 1
In the following example, two Truck objects are compared using the == operator (incorrect) as opposed to calling the equals() method (correct).
Use the equals() method to compare objects instead of the == operator. If using ==, it is important for performance reasons that your objects are created by a static factory, not by a constructor. |
This problem can cause unexpected application behavior. Comparing objects using == usually produces deceptive results, since the == operator compares object references rather than values. To use == on a string, the programmer has to make sure that these objects are unique in the program, that is, that they don't have the equals method defined or have a static factory that produces unique objects. |
Nature | Type | ID | Name | View(s) this relationship pertains to![]() |
---|---|---|---|---|
ChildOf | ![]() | 171 | Cleansing, Canonicalization, and Comparison Errors | Development Concepts699 |
ChildOf | ![]() | 569 | Expression Issues | Development Concepts (primary)699 |
ChildOf | ![]() | 697 | Insufficient Comparison | Research Concepts (primary)1000 |
ParentOf | ![]() | 597 | Use of Wrong Operator in String Comparison | Development Concepts (primary)699 Research Concepts (primary)1000 |
Modifications | ||||
---|---|---|---|---|
Modification Date | Modifier | Organization | Source | |
2008-07-01 | Sean Eidemiller | Cigital | External | |
added/updated demonstrative examples | ||||
2008-07-01 | Eric Dalci | Cigital | External | |
updated Time of Introduction | ||||
2008-09-08 | CWE Content Team | MITRE | Internal | |
updated Description, Relationships, Other Notes | ||||
2009-05-27 | CWE Content Team | MITRE | Internal | |
updated Name | ||||
Previous Entry Names | ||||
Change Date | Previous Entry Name | |||
2008-04-11 | Incorrect Object Comparison: Syntactic | |||
2009-05-27 | Incorrect Syntactic Object Comparison | |||