• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

hashCode values

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I'm studying by Kathy Sierra's book and I have a doubt regarding hashCode which contract says:

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.

but on the book's example,I added the System.out..., it shows different, could anyone please explain what I missed?



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

That is because Moof class does not override the hashCode() method. The hashCode you are getting for your Moof objects come from the default
implementation of the Object class (super class of every class in Java).

BTW "Welcome to Ranch!"
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to Javaranch Marcelo Ribeiro



When you override the equals method, then you should also override the hashCode() method in your class while defining the equality of two objects.

What's written in the book is right..

In the above code you are overriding the equals() but not the hashCode().
And when you are calling the hashCode(), the Object class hashCode() gets called..and that Object Class hashCode() always returns a unique value, even if you override the equal() yourself..

try overriding the hashCode() yourself..
[ August 04, 2007: Message edited by: Priyam Srivastava ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to use moof his hashCode you should implement both equals and hashCode methods, and I do not see an overwritten hashCode method. So in which page did you see this example, is it about hashCode or just an example of the equals method.

I think if you do not overwrite hashCode you get the pointer value of the reference, and that is not going to be equal.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the ranch. Please review the Naming Policy at the ranch. In particular, you must use a first and last name that could belong to real person. Thanks for your cooperation!!
[ August 04, 2007: Message edited by: Keith Lynn ]
 
Marcelo Ribeiro
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I appreciate the answers, I'm having a hard time studying for this test, I've being working with java for about 3 years and now I'm being "forced" to get the certification, I always rely on IDE's showing me the error never had to have the rules on mind, I used to have them at google...rs...well, now that I have to pass the exam I'll study to hard, even because my test is schedulled to 15/08.

Keith, I updated my display name.

Thank you!
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic