Why Equals() method override in Java.Reasons for it? I was not able to understand it clearly.
Asked
Active
Viewed 43 times
0
-
`equals` method is used to check for object equality, whereas `==` operator checks if objects are stored in the same place in memory (if two variables are pointing to the same object in memory). – Embid123 May 20 '20 at 09:31
-
Does this answer your question? [Java (Equals Method)](https://stackoverflow.com/questions/34411932/java-equals-method) or [equals() method of java](https://stackoverflow.com/questions/1986009/equals-method-of-java) – Sudhir Ojha May 20 '20 at 09:33
-
It helps defining the identity of your objects. Don't forget `hashCode` and possibly `compareTo`, see https://medium.com/qudini-engineering/java-object-identity-or-how-to-override-equals-hashcode-and-compareto-400fd4547fe0 for more details. – sp00m May 20 '20 at 09:33
-
Yes. Any operation involving logical comparison objects uses `equals`. By default (`Object`'s implementation), equals compares only *identity*, which is the same as using `==`. In most of our models, we consider two objects "equal" if their contents are the same, so we need to re-implement (override) `equals` to get this to work. – MC Emperor May 20 '20 at 09:58
1 Answers
0
The java string equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true.Whereas (==) compares the references.

P Yoga Prudhvi
- 107
- 2
- 3
- 11