Why do you override the equals() method? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

Why do you override the equals() method?

22nd Mar 2017, 5:01 AM
Dev
Dev - avatar
4 Answers
+ 18
Additional to Bhabeshs answer, if you override equals you have to override hashCode as well. Because if a.equals(b), a.hashCode() must be the same as b.hashCode().
22nd Mar 2017, 5:18 AM
Tashi N
Tashi N - avatar
+ 4
Because without overriding it works same as == operator which check for reference not for content equality.
22nd Mar 2017, 5:10 AM
Bhabesh Gogoi
Bhabesh Gogoi - avatar
+ 4
@Tashi, Congratulations for Moderator badge. You really deserved it :-)
22nd Mar 2017, 9:58 AM
Meharban Singh
Meharban Singh - avatar
+ 1
That's perfect Tashi. (y)
22nd Mar 2017, 5:22 AM
Bhabesh Gogoi
Bhabesh Gogoi - avatar