0

I have

Object o = new Object()
o == o.clone(); //I understand this should be false since it's different two arraylist pointing to the same object right?
o.equals(o.clone())  //it's true if it's shallow copy since pointing at the same one

here equals isn't ==, it should be comparing hash code right? since it's object. source: equals method usage in string and list

but on my notes it says the equal method means == here.

is my notes wrong?

my note says the object class has aclone() method but it has an empty implementation, and an object of the object class is not allowed to invoke this method because of this reason.

Can someone please explain this b etter? I think I misunderstanding something

Si Yo
  • 67
  • 6
  • @Krease So according to the link equals mean == but I learnt equals in objects compare hashcode but == means checking the value no? – Si Yo Dec 07 '18 at 20:46
  • `equals` doesn't *"mean"* `==`. `equals` is just a method to enable objects to compare themselves with other objects. Its *default implementation* (i.e. `Object.equals(Object)`) is to check for *identity* (i.e. `==`). – MC Emperor Dec 07 '18 at 22:12
  • *"here equals [...] should be comparing hash code right?"* No, `equals()` doesn't compare hash codes. It doesn't what you coded it to do, assuming you implemented the method on your class. With `Object` class, `equals()` is implemented to use `==`, so for `Object` (and any class without `equals()` implementation, `equals()` is the same as `==`, but it (should) never compare hash codes. – Andreas Dec 08 '18 at 00:19

2 Answers2

1

== compares object reference - are they the exact same memory reference. equals is a method implemented per class, typically to do more useful comparison (like if two strings contain the same characters, or if two lists contain the same content).

However, the default implementation in Object simply falls back to doing an == comparison, so for instances of that class, there is no difference.

Krease
  • 15,805
  • 8
  • 54
  • 86
  • that's the opposite of https://stackoverflow.com/questions/53661319/equals-method-usage-in-string-and-list..... – Si Yo Dec 07 '18 at 20:44
  • 2
    What? That answer is incorrect about `hashCode` (I've commented on it, and expect it will be fixed soon) - comparing `hashCode` values is NOT the default implementation of `equals`, and should NEVER be the implementation of `equals` - it is possible to have multiple non-equal objects with the same result for `hashCode`. That said, whenever overriding `equals`, you should always override `hashCode` so that objects that **are equal** generate the same hash. Please read [this](https://stackoverflow.com/questions/7520432/what-is-the-difference-between-vs-equals-in-java) as a primer. – Krease Dec 07 '18 at 22:15
  • I've added my own answer to your other question, hope it proves useful in explaining this concept. – Krease Dec 07 '18 at 22:26
0
  • == checks object identity in the context of object references.

    Foo f1 = new Foo();
    Foo f2 = f1; // Both f1 and f2 refer to the same object.
    // f1 == f2 returns true
    Foo f3 = new Foo(); // A second instance of Foo is created
    // f1 == f3 returns false
    

    Only in the context of primitive data types (boolean, byte, short, int, long, char, float and double) == checks for value.1

  • The equals method is nothing more than a method to enable objects to compare themselves with other objects. The documentation of equals sets requirements to how the programmer should implement the method, but technically the programmer could do anything he wants.

    The implementation of Object.equals(Object) is simply return this == obj).

  • The equals method should never rely on nor call hashCode(), since it's possible that two objects which are considered unequal according to equals could have the same hash code. The only requirement of hashCode is the other way around: that it must return the same hash code for all object considered equal by means of the equals method.


1 As Andreas already mentioned in the comments – technically == always checks value. This also counts for object references, since object references themselves are values.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
  • Technically, `==` always checks for value, its just that if the value is a reference, it is the reference value (object identity) that is being checked. – Andreas Dec 08 '18 at 00:22
  • @Andreas You're right, just as Java is always pass by value. I'll add that to my answer. – MC Emperor Dec 08 '18 at 00:27