• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

K &B Mock exam Question doubt

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What code can be inserted in equals method so that it fulfills the hashcode contract?
Two correct answers are-
1. return((SortOf)o).code.length()*((SortOf)o.bal)*((SortOf)o.rate==this.code.length()*this.bal*this.rate;

2. return((SortOf)o).code.length()*((SortOf)o.bal))==this.code.length() *this.bal.

I am not able to understand how 1 option is also correct.
It is possible that equals method can be return true while hashcode is still not equal as we can have such combination of length*rate.
Please clarify.



--- indenting repaired, Bu.
[ September 13, 2007: Message edited by: Burkhard Hassel ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by neha verma:



Hi!
I think option number 1 is also correct because the hashcode contract (K&B book, chapter 7, page 534) says "If two object are equal according equals(Object) method, then calling hashCode() method on each of the two objects must produce the same integer result."
[ September 13, 2007: Message edited by: Burkhard Hassel ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Neha,

no, answer one does not violate the contract.
Condensed version:

equals: code, bal & rate.
hash: code & bal



if eqauls is true, the hash code will be the same.
Contract fulfilled.

if equals is false because of rate is different, the hashcode is still the same, but this does not violate the contract.

The contract says only that when equals() returns true, hashCode() must be the same int.
It does NOT say the opposite (<WRONG> when equals returns false, hashCode() must return different ints. </WRONG> ).





Yours,
Bu.

By the way, I isolated the "forbidden word" of your posting in code tags but "uncoded" the question part to avoid the very long lines. Hope this is OK for you.
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

if for Obj, length = 2, bal = 3, rate = 4 => 2*3*4=24
and Obj 2, lenght = 1, bal = 3, rate = 8 => 1*3*8=24

then both objects should be equal,

but the hashCode algorith: length* bal
would be calculate respectively to 2*3 = 6
and 1*3 = 3,
which would mean that the contract is not respected, as the 2 objects are equal but their hashCodes are not the same

So I don't see how 1 would be true.

Please help me to understand

Thanks
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Katrin:

then both objects should be equal,



No.

If hashCodes are equal for two objects, then they do NOT have to be equal regarding to the equals() method.

equal true ---> hashCode same


The arrow to be read as "implies".

You cannot just turn the direction of the arrow when you want to obey the rules of logic.
Every day example why you can't:

it is raining ---> earth is wet
Treat this as true, you cannot be sure that

it is raining <--- earth is wet
is also true, because the wet earth could have another cause.


Yours,
Bu.
 
Josh Bag
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:
Katrin:

No.

If hashCodes are equal for two objects, then they do NOT have to be equal regarding to the equals() method.

equal true ---> hashCode same


The arrow to be read as "implies".

You cannot just turn the direction of the arrow when you want to obey the rules of logic.
Every day example why you can't:

it is raining ---> earth is wet
Treat this as true, you cannot be sure that

it is raining <--- earth is wet
is also true, because the wet earth could have another cause.


Yours,
Bu.



I don't agree with you,...Katrin have an example in which two object (using equal method lenth*bal*rate) are equals, so i think hashcode method must return the same value (if equal method is a valid one) but it isn't so. Therefore, the first equals method in the exercise doesn' fulfill the hashcode contract!



Please, help us bcz I'm a little confused.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bukhard,
I dont agree with you, what example Katrin has given means that two objects are equal but still their hashcodes are not equal.
So how does 1 option fulfill the hashcode contract.Can you please explain.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha, first of all, note that the hashCode() method must be written with a capital C. If you don't, then you are not overriding hashCode() in class Object, but you are just adding a new method called hashcode() to your class which does not have anything to do with the hashCode() method that Java uses.

Note that the @Override annotation exists to protect you from such mistakes. If you would have used it like this:

Then you would have gotten a compiler error. The @Override annotation checks if your method really overrides a method from a superclass and gives an error if it doesn't. So it's good to teach yourself to always use @Override on methods that are supposed to override methods in superclasses.

Now back to the original question...: I think Burkhard misunderstood Katrin. Katrin is right.

The contract for hashCode() is this: If equals() returns true when you compare to objects A and B, then the hashcodes of A and B must be the same. (Note that this does not mean that when equals() returns false, the hashcodes must be different - they might still be the same. It only works in one direction).

Katrin's example demonstrates that answer 1 is wrong: with different combinations of code.length(), bal and rate, you can get the answer true from equals(), but hashCode() will return different values. That's breaking the hashCode() contract.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the clarification. So this is a wrong answer given in mock Master exam that comes alongwith K&B book. Correct.
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by neha verma:
Thanks for the clarification. So this is a wrong answer given in mock Master exam that comes alongwith K&B book. Correct.



Thought I'd bring up something from the Head Rush book pg 560-562. It talks about Reference Equality vrs. Object Equality. According to the book, Reference Equality is synonymous with the hashcode value of the object computed by the collection. Object Equality means that two objects can be equal, even though they have different hashcodes returned by the default equals() method. To do this, you *MUST* override the both the equals() and hashCode() methods. However, the equals() method doesn't have to return the same value as the hashCode() method.

Example code snippet:



In this case, we want equals() to return false if the two Song objects' title instance variables are different.

From my understanding of this description, 1 is correct and I'm with Bu.

@Josh: I think you may have misread Bu's reply on this. He's saying 1 is
correct.

Aloha,
Doug
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any more thoughts on this.
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look from the equal method's perspective not from hash code method.
Whatever can affect hash code method should also effect equals method.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Neha,

Can you confirm that there isn't a typo in your snippet of code. Should it read hashCode and not

hashcode

?

Happy Studying
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,

Please can you confirm if there's a typo. Otherwise, please confirm where exactly you got the question from. I've got th K&B book and I'm preparing for SCJP 1.4 and want to make sure we've got the correct answer on this one.

Happy Studying
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals & hashcode method in Object class is little tricky to implement. The best place to understand this is to go through the API.

Do let me know if you need any more clarifications after going through the API description and I'm happy to explain it further.
 
neha verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Annette Sovereign -Yes there is a typo error in the code above- its hasCode() method not hashcode(). It is my mistake. This is a question from K&B mock exam.
Krithika -I am still not clear about the fact that why answer 1 is also correct. As we can prove that when are equals method returns true the hashCode() method may fail. So does'nt it violate the hashCode() contract.
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for getting back to me.

its hasCode() method not hashcode().




Do you mean its hashCode() method not hashcode(). ()

Sorry to be pendantic but it does have implications for the way the question is answered.

Happy Studying
 
You ought to ventilate your mind and let the cobwebs out of it. Use this cup to catch the tiny ads:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic