Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: incomplete link MSC03-J fixed.

...

The cause of this erroneous behavior is that the CreditCard class overrides the equals() method but fails to override the hashCode() method. Consequently, the default hashCode() method returns a different value for each object, even though the objects are logically equivalent; these differing values lead to examination of different buckets in the hash table, which prevents the get() method from finding the intended value.
Note that by specifying the credit card number in main(), these code examples violate rule MSC03-J. Never hard code sensitive information for the sake of brevity.

Compliant Solution

...

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="93beb2ca7eee1d6b-5eeeda8b-48624c15-b9c09d89-34f3c1d446fa7d3d359fd83e"><ac:plain-text-body><![CDATA[

[[API 2006

AA. References#API 06]]

[Class Object

http://java.sun.com/javase/6/docs/api/java/lang/Object.html]

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

<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="7242efb1b81fc53b-bf31300d-4b674b7c-96f6a4d9-e13ad7cfa0bbcca515012b88"><ac:plain-text-body><![CDATA[

[[Bloch 2008

AA. References#Bloch 08]]

Item 9. Always override hashCode when you override equals

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

...