11.4 Comparing Objects and Establishing Order
The Object method equals(Object) provides information about whether two objects have the same state but says nothing about which object is “bigger” or “smaller.” However, in many applications, the order of objects plays a role. This need is obvious in sorting operations, but also in simpler questions, such as finding the largest or smallest item in a collection. If objects are to be compared in Java, these objects must always be arranged in a certain order. The system can never decide on its own, and often, multiple criteria determine a sort order. For example, why is one candy “smaller” than another candy? Perhaps one candy has 20 calories and the other has 600 calories, or maybe because they ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access