• 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

Question from Kathy and Bert's book - p.413

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On p. 413:
"if two objects are considered equal using the equals() method, then they must have identical hashcode values. So to be truly safe, your rule of thumb should be if you override equals(), override hashCode() as well."
But what about the indexOf() method in the Vector class. This method tests for equality using the equals() method. I'm assuming that you need to override this method for it to work correctly. In this case, would you need to override the hashCode() method as well?
Mansi
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To properly follow the hashcode/equals contract, you should properly override both so that equal objects have the same hashcode values
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I override both methods and I don't follow the contract, will the compiler or JVM give an error ?
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by bennido kool kat:
If I override both methods and I don't follow the contract, will the compiler or JVM give an error ?


No. As long as you do a correct override (same return type, no new check exceptions, public access modifier, etc) It will not complain at all. you will only get inconsistent results in your application.
[ July 10, 2003: Message edited by: Andres Gonzalez ]
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


If I override both methods and I don't follow the contract, will the compiler or JVM give an error ?


No it won't give an error. The contract is an unofficial thing. A convention in a way. You can override them to do anything you like, but if you don't follow the contract then you may find design problems with your code, especially concerning collections.
K&B discuss some examples in their book. I'm too lazy to find any at the moment.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic