| Author |
doubt about MasterExam question
|
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
Here there is the question in the MasterExam: Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all that apply) A: return ((SortOf)o).bal == this.bal; B: return ((SortOf)o).code.length() == this.code.length(); C: return ((SortOf)o).code.length()*((SortOf)o).bal == this.code.length(); D: return ((SortOf)o).code.length()*((SortOf)o).bal*((SortOf)o).rate == this.code.length()*this.bal*this.rate; ANSWER : C and D I'm not agree with D because i made an example where SortOfObject1.equals(SortOfObject2) and SortOfObject1.hashCode()!=SortOfObject2.hashCode() My example: the output is : true 15 30 Maybe I'm wrong with something, please tell me. Thank you [ March 12, 2008: Message edited by: Alain Sellerin ]
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1402
|
|
Technically none of them do, as for any null reference that is passed as an argument to equals(), instead of returning false as the contract for equals specifies, these implementations all throw a NullPointerException. As the contract for equals() is never completely satisfied for any implementation, I'd say the question is flawed. [ March 12, 2008: Message edited by: Jelle Klap ]
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Niala Nirell
Ranch Hand
Joined: Mar 12, 2008
Posts: 46
|
|
|
thank you for your comment
|
 |
 |
|
|
subject: doubt about MasterExam question
|
|
|