This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes contract for equals and hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "contract for equals and hashcode" Watch "contract for equals and hashcode" New topic
Author

contract for equals and hashcode

Nisha Jasiwal
Greenhorn

Joined: Jan 21, 2009
Posts: 2
Hi,
I am new to this forum.......& this is my first post.........

Please describe me the reason why the option 3 & 4 are correct?


class SortOf {
String name;
int bal;
String code;
short rate;
public int hashCode() {
return (code.length() * bal);
}
public boolean equals(Object o) {
// insert code here
}
}

Which of the following will fulfill the equals() and hashCode() contracts for this class? (Choose all that apply.) 
1. return ((SortOf)o).bal == this.bal;
 2. return ((SortOf)o).code.length() == this.code.length();
 3. return ((SortOf)o).code.length() * ((SortOf)o).bal == this.code.length() * this.bal;

 4. return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate == this.code.length() * this.bal * this.rate;
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Hi Nisha! Welcome to javaranch.

here we have a rule that if you post any question, then you have to QuoteYourSources. So please tell us from where did you copy this question...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Also please use code buttons..........


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1794
    
    7

The question is flawed, because none of the specified answers actually fulfill the equals() contract properly, as the equals() contract states:

For any non-null reference value x, x.equals(null) should return false.

This requirement clearly does not hold for any of the answers, as they will in fact throw a NullPointerException in such a case.


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.
Nisha Jasiwal
Greenhorn

Joined: Jan 21, 2009
Posts: 2
Hi Ankit

I got these lines of code from a exam simulator LearnKey which I got along with SCJP1.5 of kaithy sierra's book.


Ankit Garg wrote:Hi Nisha! Welcome to javaranch.

here we have a rule that if you post any question, then you have to QuoteYourSources. So please tell us from where did you copy this question...
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Nisha Jasiwal wrote:Hi Ankit
I got these lines of code from a exam simulator LearnKey which I got along with SCJP1.5 of kaithy sierra's book.

Now that you have understood how to use the quote button, there is a code button just to the right of the quote button.
So why don't you edit your 1st post to use code buttons, so that it becomes more readable...........
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Well this is an error in the master exam and the correct answer is only 3...
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
What if null is passed to the equals method???
i mean Object o= null
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Kedar Nath wrote:What if null is passed to the equals method???
i mean Object o= null


It depends on the equals method implementation. The equals method of Object class will return false in that case. I hope you are talking about this

nonNullReference.equals(null); //false

but if you call this

Object o = null;
o.equals(anyObject);//NullPointerException
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
So in the above question all of them should result to such a case ( NUll pointer), if a null is passed.
So none of them are right. Isn't it/ :?:
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Kedar Nath wrote:So in the above question all of them should result to such a case ( NUll pointer), if a null is passed.
So none of them are right. Isn't it/ :?:


How can you say none of them is right. It is true that since there is no check so if you pass null into the equals method, then it will result in a NullPointerException. But this doesn't mean that the option is wrong...
Kedar Nath
Ranch Hand

Joined: Sep 02, 2008
Posts: 53
But we cant say that the option are right.because question says
Which of the following will fulfill the equals() and hashCode() contracts for this class?


Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Well the hashCode and equals contract is fulfilled in this case. If you read the documentation, then you will find that it doesn't say anything about null references passed to equals. The contract is for non-null references. Also if you pass a null reference to the equals method, then no doubt it will throw a NPE, but the hashCode method for that null reference will also throw NPE. So the behavior is same for both of them...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1794
    
    7

Ankit Garg wrote:Well the hashCode and equals contract is fulfilled in this case. If you read the documentation, then you will find that it doesn't say anything about null references passed to equals. The contract is for non-null references. Also if you pass a null reference to the equals method, then no doubt it will throw a NPE, but the hashCode method for that null reference will also throw NPE. So the behavior is same for both of them...


The constract does in fact explicitly mention how null references should be handled.
Take a look at the API documentation for java.lang.Object#equals(Object).
The last bullet says:

For any non-null reference value x, x.equals(null) should return false.

This requirement is violated by all the provided solutions.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

Oh yes! I missed that. But I don't think that this was in the mind of the authors...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1794
    
    7

Ankit Garg wrote:Oh yes! I missed that. But I don't think that this was in the mind of the authors...


I definitely agree, but exam questions really shouldn't be ambiguous
So either the question should prashed more carefully, or the provided answers should be altered to comply with the full contract of the equals() method.
I'd personally prefer the latter.
 
Consider Paul's rocket mass heater.
 
subject: contract for equals and hashcode