Guest User

Why is .compareTo() in an interface while .equals() is in a

a guest
May 17th, 2016
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. The early [JLS 1.0 §4.3.2](http://titanium.cs.berkeley.edu/doc/java-langspec-1.0.pdf) defines the class `Object` in the following way:
  2.  
  3. > 4.3.2. The Class `Object`
  4.  
  5. >The standard class `Object` is a superclass (§8.1) of all other classes. (...) All class and array types inherit the methods of class `Object`,
  6. which are summarized here (...)
  7.  
  8. > The members of `Object` are as follows:
  9.  
  10. > The method `getClass` returns the `Class` (§20.3) object (...)
  11.  
  12. > The method `toString` returns a `String` representation of the object.
  13.  
  14. > The methods `equals` and `hashCode` are declared for the benefit of hashtables
  15. such as `java.util.Hashtable` (§21.7). The method equals defines a notion
  16. of object equality, which is based on value, not reference, comparison.
  17.  
  18. > The method `clone` is used to make a duplicate of an object.
  19.  
  20. > The methods `wait`, `notify`, and `notifyAll` are used in concurrent programming
  21. using threads, as described in §17.
  22.  
  23. > The method `finalize` is run just before an object is destroyed and is
  24. described in §12.6.
  25.  
  26. So, that's why `equals` is in `Object` but `compareTo` is in a separate interface. Java devs included only the methods immediately needed for all uses intended by then. To be hashable, every `Object` would need `equals` and `hashCode` - but not all objects would need to have a concept of **ordering**, which is what `compareTo` is used for.
RAW Paste Data