equals : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

equals
A method of Object for comparing two objects. If you don’t override equals, the default version simply compares the references, in other words even if all the fields in the two objects are identical the objects will still not compare as equal. They will compare as equal only when both references point to precisely the same object. This default equals is usually useless as the proverbial tits on a bull. You have to write your own equals to compare field by field, ignoring irrelevant fields. You can override equals like this:

Strategy

Here is a strategy for writing an equals method for some Object:
  1. Check for equality of the cached hashcodes. If they are not equal you know right away the two Objects can’t be equal.
  2. You then compare the two addresses/references. If equal, you know the entire Objects must be equal (the same).
  3. Failing that, you check the Object sizes. If they are not the same you know the two can’t be equal. There is no method to give you the aggregate Object size. By that I mean the sum of the lengths of the variable parts of the Object that participate in the equality test.
  4. Failing that, you do your byte by byte comparison.
Whenever you implement equals, you must also implement a matching hashCode.

Learning More

Oracle’s Javadoc on equals package : available:
Oracle’s Javadoc on Arrays.deepEquals() : available:

This page is posted
on the web at:

http://mindprod.com/jgloss/equals.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\equals.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[54.172.169.199]
You are visitor number