• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashcode & equals question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got this question from Practice exam of Kathy Sierra book CD.
Q-52

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

Which of the following would fulfill the equals() and hashCode() contracts of the class?

A & B irrelevant
C. return ((SortOf)o).code.length() * ((SortOf)o).bal == this.code.length()* this.bal;

D. return ((SortOf)o).code.length() * ((SortOf)o).bal * ((SortOf)o).rate== this.code.length()* this.bal * this.rate;

The answer given is C & D.
but in D, isn't it possible for the hashcode to be not equal, and cause equals() to return true? Isn't that a violation of the contract?
eg:-
Sortof1 - code.length() = 2
bal =3
rate = 4
Sortof2 - code.length() = 3
bal = 4
rate = 2
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Maleen, welcome to javaranch...

Yes Maleen, you are right. This is an error in that question. The answer is indeed C only....
 
Maleen Abeydeera
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your prompt answer Ankit. I have another question on this topic.

Is this equals() and hashCode() contract something you must adhere to? I mean, can there be a legal program such that
x5.equals(x6) == true
but x5.hashcode() != x6.hashcode() ?

It occurred to me from Q-53 of the same exam.
According to the answer,
if x5.equals(x6) == true , then
x5.hashcode() == x6.hashcode() will "always" be true
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well if you don't follow that contract, you might face unexpected outputs when you use hash based collections like HashMap or HashSet etc.

Then you will not be able to find objects which are meaningfully equal...
 
Maleen Abeydeera
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, apart from the unexpected output, is there anything that will not work?

i mean, for the exam, are we supposed to treat this in the same league as a compilation error or an exception?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No I don't think exam question will treat this as an error. There are questions like

Assume that XYZ class implements equals and hashCode correctly. Then what will happen when you run this code



or like this

what will happen when you run this program



and the program will not fulfill the contract so the output will be unexpected. But this doesn't mean that it will be an error. You have to choose the correct output which will come when the contract is broken..
 
Maleen Abeydeera
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, but what if it's a direct question?

like

if x5.equals(x6) == true , then is
x5.hashcode() == x6.hashcode() will "always" be true ?

without saying anything about whether the contracts are followed
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Its not neccessary for them to be true. You can break the contract and still run it.

In your previous post, Contract is broken in the code but the program runs with unexpected output.

But as you know, its always advisable to stick to the contract
[ November 10, 2008: Message edited by: Himalay Majumdar ]
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic