3

The equals method of Object just compare address:

public boolean equals(Object obj) {  
    return (this == obj);  
}

I think it's not useful in most cases, and we may override it. But for the most Classes I developed, I didn't override the equals method, because I won't use it at all...

So I just wonder, why Java language designer put equals method in Object? Why there isn't an "Equalable" interface like Comparable?

Sayakiss
  • 6,878
  • 8
  • 61
  • 107
  • Even for objects that don't override, knowing that a single object is always equal to itself is thoroughly useful. – chrylis -cautiouslyoptimistic- Feb 24 '15 at 10:41
  • 1
    Possibly the designers thought that the default `equals` (and `hashCode`) behaviour was generally usable (people can override it where required), whereas a default `compareTo` implementation would not be generally useful, because there isn't really a natural ordering for general objects. – khelwood Feb 24 '15 at 10:41
  • 2 things can either be equal or non-equal. they may not be comparable but still that means they are not equal. – Ankit Feb 24 '15 at 10:52
  • *"because I won't use it at all"* - you don't use any collections in your code? – user11153 Feb 24 '15 at 11:17

3 Answers3

4

The equals() method is used by the Java system classes, for example in HashMap. Since every object may be stored in HashMap, every object needs an equals() method. The default implementation is sufficient for this.

This is just one example. There are lots of places equals() is called.

Thomas Stets
  • 3,015
  • 4
  • 17
  • 29
  • 2
    Every object can also be placed in a `TreeMap` - yet, it is not `Comparable` (and an exception will be raised) `Set s = new TreeSet<>(); s.add(new Object());` will compile just fine – amit Feb 24 '15 at 10:43
  • Having a equals() and hashCode() method in every object was a design decision made years before there was TreeMap. – Thomas Stets Feb 24 '15 at 10:48
  • 1
    @amit HashMaps and Lists are used way more often than TreeMaps. `equals` also has a sane default implementation, where there is none for `compare`. It would be very inconvenient to add boilerplate default `equals` methods to almost every class - or create wrapper for 3rd party classes – kapex Feb 24 '15 at 10:50
  • @kapep I criticized the argument of `Since every object may be stored in HashMap, every object needs an equals() method` Boiler plate is a different argument than "you need to support all objects, because every object can be inserted", and the counter example is the Comparable. – amit Feb 24 '15 at 10:53
  • @amit ok I misunderstood. A treeset only accepts all objects because you can also provide a comparator to make it work for non-Comparables though. `equals`-based collections could have done the same by providing a (default) "equality-comparator" (not sure how to name that). But I think it's bad design in the first place, to mix both comparator and comparable approach in the same collection type. – kapex Feb 24 '15 at 11:21
4

Identity provides a universal definition of equality. Every object is equal to itself. It may or may not be logically equal to some objects that are not itself. If it is, override equals and hashCode. If not, inherit the Object ones.

That is very different from being Comparable. It is possible for a structure that might be represented by a class to lack any meaningful total order - consider the complex numbers.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
1

If Java had the interface "Equalable" like the interface "Comparable" it would not be mandatory to include it in every object in Java, so there might arise new problems when adding an object into a Collection and so on.

And of course the hashcode + equals contract paradigm would be broken

Please check this link from effective Java

http://www.ideyatech.com/2011/04/effective-java-equals-and-hashcode/

JoSeTe4ever
  • 473
  • 6
  • 17
  • The same applies to comparable. `Set s = new TreeSet<>(); s.add(new Object());` will compile just fine, but throw an exception. – amit Feb 24 '15 at 10:45
  • @amit But then you would have to use custom "equalators" when creating every collection. – user11153 Feb 24 '15 at 11:21