Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

hashcode contract S&B question

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is taken from the Sierra and Bates mock exam that comes with the SCJP 1.6 book

Given:-

Which of the following when inserted will fufill equals and hascode conrtacts for this class

(a) return ((SortOf)o).bal == this.bal
(b) return ((SortOf)o).code.length()= =this.code.length()
(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

I think the answer d is incorrect if you consider the following scenario

object 1 has code.length =2, bal=3 and rate=4
object 2 has code.length =4, bal=2 and rate=3

in this case their hashcodes will be different 2*3 != 4*2
but equals will return true 2*3*4 == 4*2*3
which breaks the hashcode equals contract

Therefore c is the only correct answer

Is this right or am I missing something?

Martin
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:You are correct



Does it mean, only fields used in evaluating hashCode should be used in equals method?
Please clarify.
 
Sheriff
Posts: 9691
42
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

janardhan nanduri wrote:Does it mean, only fields used in evaluating hashCode should be used in equals method?


Not necessarily. You just need to make sure that the hashCode and equals contract is fulfilled that's all. Example
 
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You are right Martin . Only option C is correct.
I had also faced problem in the same question . I had chosen only option C.
 
reply
    Bookmark Topic Watch Topic
  • New Topic