equals : Java Glossary

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:

CMP homejump to top

available 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
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Blog
IP:[65.110.21.43]
Your face IP:[107.21.156.140]
You are visitor number 40,264.