Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

As a general rule, use the Object.equals() method to check whether two objects are abstractly equal to each other. Reserve use of the equality operators == and != for testing whether two references specifically refer to the same object. (This is reference equality.) Also see guideline rule "MET13-J. Classes that define an equals() method must also define a hashCode() method."

This use of the equality operators also applies to numeric boxed types (for example, Byte, Character, Short, Integer, Long, Float, and Double), although the numeric relational operators (such as <, <=, >, and >=) produce results that match those provided for arguments of the equivalent primitive numeric types. See guideline rule "EXP03-J. Do not use the equality operators when comparing values of boxed primitives" for more information.

...

The Coverity Prevent Version 5.0 BAD_EQ checker can detect instances where the == operator is being used for equality of objects when, ideally, the equal method should have been used. The == operator could consider the objects to be different whereas the equals method would consider them to be the same.

Findbugs checks this guideline rule for type String.

Related Guidelines

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="3a5acab4dc4e6321-fb350f82-4f7443bf-9205b865-2571484d5635262210d270b7"><ac:plain-text-body><![CDATA[

[[FindBugs 2008

AA. Bibliography#FindBugs 08]]

ES: Comparison of String objects using == or !=

]]></ac:plain-text-body></ac:structured-macro>

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="424d489f17e04422-2f814e93-464d430d-88dbaa60-a89395794985490b7365a2b0"><ac:plain-text-body><![CDATA[

[[JLS 2005

AA. Bibliography#JLS 05]]

[§3.10.5, "String Literals"

http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.5]

]]></ac:plain-text-body></ac:structured-macro>

 

§5.6.2, "Binary Numeric Promotion"

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="5b0b130bbd78d881-39b88207-47474f70-85779a72-05c6b4b91f06ac6564be8b8b"><ac:plain-text-body><![CDATA[

[[Rogue 2000

AA. Bibliography#Rogue 2000]]

Rule 79: Use equals(), not ==, to test for equality of objects

]]></ac:plain-text-body></ac:structured-macro>

...