• 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 contract

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While reviewing the questions I had wrong on the
exam on the cd that comes with the K&B book (test A,
I don't see the question number), I do not understand
one of the answers.



The question is to select all lines that will fulfill the
equals() and hasCode() contracts for this class.

My answer was:
C: check whether product of code.length() and bal is equal.

But MasterExam said also D is correct:
D: check whether product of code.length(), bal and rate is equal.

The answer further explains that the equals() method must be as least
as precise as the hashCode() method is.

I don't understand (maybe I should go to sleep
My problem is the following:

Suppose I have two objects, which are equal because they
both have rate == 0. Those objects can easily have different
hashCodes. Hence, the hashCode contract is violated, right?
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

As per the contract, the equals() method must be as least as precise as the hashCode() method is.

In the code above, hashcode() uses 'code' and 'balance' variables. So the equals methods must have both 'code' and 'balance' variables. That is the minimum requirement. Apart from this, you can also add other variables of the class in the equals(). Eg: You can use:
1) code, balance and rate
2) code, balance, rate and name

Once again, the equals method must have atleast code and balance.

Hope this helps.
 
John Sutt
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply, but I stil don't get it.

Page 554 of K&B Book SCJP6 says (part of hashCode() contract):
"If two objects are equal according to the equals(Object) method, then calling the hashCode() method on each of the two objects must produce the same integer result".

So I have two objects:
Object 1 has: name="Kathy", bal = 5, code="SCJP5", rate=0.
Object 2 has: name="Bert", bal = 6, code="SCJP6", rate=0.

Let's compute the hashCode()'s now:
Object 1 has: code.length() * bal = 5 * 5 = 25.
Object 2 has: code.length() * bal = 5 * 6 = 30.

Object1.equals(Object2) returns true, because both
sides of the equality == are multiplied by 0 !

Hence, equal objects result in different hashCodes and the
contract is violated.

The problem with this exercise is that simply multiplying additional
instance variables does not make the comparison more precise.

I was able to construct a Set and add the above two objects, resulting
in a set with two elements (instead of only 1, duplicates should not
have been allowed).

Do you agree the answer to the question is wrong now?
(I think the question number is 52 in K&B cd MasterExam A).
 
John Sutt
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I should have searched the forum before posting.
It seems I'm not the first one reporting that answer C is
the one and only correct answer to this question.

(If you're curious, search the forums for 'SortOf').

Answer D is incorrect as it breaks the hashcode contract.
I just wanted to hear something say I was right, to make
me feel better. But I found that now, so I'm ready for
the exam
[ August 18, 2008: Message edited by: John Sutt ]
 
I guess everyone has an angle. Fine, what do you want? Just know that you cannot have this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic