tag:blogger.com,1999:blog-5638372.post109504066780968596..comments2023-10-10T05:22:56.347-05:00Comments on binkley's BLOG: More on domain objectsBrian Oxleyhttp://www.blogger.com/profile/06617364377560752378noreply@blogger.comBlogger2125tag:blogger.com,1999:blog-5638372.post-1095170615862963812004-09-14T09:03:00.000-05:002004-09-14T09:03:00.000-05:00Emerson, your observations on equals v. compare ar...Emerson, your observations on equals v. compare are apropos, but your efficiency comment seems premature (to honor Knuth). In general, you should not try to figure out how an object external to you measures equality -- just call it's equals method and rely on that object to figure out for itself optimizations.Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-1095113062009651422004-09-13T17:04:00.000-05:002004-09-13T17:04:00.000-05:00Actually there is another very important semantic ...Actually there is another very important semantic and logical reason for having an equals() method. Identity is always required, and at the very least you will need a compare() method which returns -,0,+ so that you can perform lexical compairson on objects. Lexical comparison lets you implement ordered collections and efficeint search algorithms. <br /><br />Generally speaking equals() is redundant when you have compare() becuase compare() == 0 is the same as equals(). Or is it ?<br /><br />Equality and comparison are in fact very different concepts, particulary from an optimisation perspective. When comparing two strings in english you must lexically examine the characters in each string from left to right to determine the correct result with respect to ordering. <br /><br />If you are only interested in equality you can perform a much more efficent comparison by first checking that the lengths of the two strings are not different, and then perhaps if the strings you are comparing are URL's you might want to compare in reverse order since the end of the string is where the entropy will be greatest.<br /><br />So from an implementation and performance perspective equals() is a necessary addition to any good API.Emersonhttps://www.blogger.com/profile/00096723206012357237noreply@blogger.com