Equal method help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Equal method help

are the steps for equal method always in this manner?check reference,then null, class,then class object...is it fixed?why most of the examples i found in the internet use similar steps https://code.sololearn.com/c4N53fpRFu70/?ref=app

1st Nov 2017, 11:51 PM
oyl
6 Answers
+ 3
The order of reference or null check doesn't matter. Although, you do need to do the null check before the class check, and you need to do a class check before a downcast. This is just to avoid errors.
2nd Nov 2017, 1:51 AM
Rrestoring faith
Rrestoring faith - avatar
+ 2
Ya but you can simplify by just calling the super!
2nd Nov 2017, 12:46 AM
Chriptus13
Chriptus13 - avatar
+ 2
Because its the equals of two object but you can avoid those things!
2nd Nov 2017, 7:09 AM
Chriptus13
Chriptus13 - avatar
+ 2
We are giving our own interpretation of equality, that is what the steps are for. If the two objects are equal with ==, then we know the equality of them is the same. If the Object is null, we know it is definitely not equal. Plus if we tried to downcast a null Object, we would get an exception. etc..
2nd Nov 2017, 2:35 PM
Rrestoring faith
Rrestoring faith - avatar
+ 1
i thought the use of overriding equal method is to give our own interpretation of equality?why does it still has to follow the series of steps?
2nd Nov 2017, 7:04 AM
oyl
+ 1
thankyou so much u are so helpful
2nd Nov 2017, 2:37 PM
oyl