-1

Why would somebody use .equals(Object) method instead of “==” for non-string objects? Just because we can override .equals(Object)?

ruvinbsu
  • 115
  • 1
  • 5

1 Answers1

4

"==" compares the identity of the two objects (memory address). If you have two person objects, they have the same first last names, same age, gender..., then they SHOULD be the same person. But since you created two person objects separately in your code, when you use "==", you will conclude they are two different persons. But if you override "equals" then you can conclude they are the same person.

Check out "Effective java" (one of the best Java books out there if not the best). There are a few good items on this.

Hua
  • 666
  • 2
  • 9
  • 21