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="f17c6e339b5c193b-efbdb55f-4ece454c-9795be69-e15671882696c1fc6b3d484c"><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="2401ac56204c29b2-a19a85f7-45554eae-8683a1ca-8c815c0942add97a11adf02d"><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="4711b4261b1187ef-adfc0bb8-440e4f87-b48eb625-fd33e8a536ff8a48d8b93e74"><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>

...