• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

HashCode Question

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


what is the appropriate definition of hashcode method in Person class?
1.return super.hashCode()
2.return super.hashCode()+age*7
3.return super.hashCode()+name.hashCode()/2
4.return super.hashCode()+name.hashCode()/2-age*3

Answers shown is 2.i got these question from actuals Test.Please tell me how it comes ?

Thanks In Advance
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have gone with 4th option as it involves both age and name as in the given equals() method.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I too wondered How the answer is 2.

According to me the correct answer will be 4. because thats the only one which fulfills the hashcode contract !!

May be some printing mistake !
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read in K&B that if equals method is overridden then hashcode method should also be overridden although it is not a rule , although I couldn't answer this question, can anyone tell me what @override do, I saw it in netbeans but it doesn't come along with any documentation, , please explain althogh it is not a thread topic.
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
I think we need to override the hashcode to get correct implementation,
here for every new object hashcode will be different, so even if equals say objects are equal hashcode will be different, which is wrong according to their contract,


@override

this kind of syntax is called as annotations,used to give information to compiler.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each of the proposed answers involves super.hashcode(). Since "super" in this case is Object, that means we would be using Object's hashcode function. But that returns a value which attempts to be distinct for every object. Therefore none of the expressions is likely to return the same hashcode for equal Person objects.
 
Master Rancher
Posts: 5120
82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Sarath]: Answers shown is 2.i got these question from actuals Test

Um, I doubt that. The actual test doesn't show you the expected answer. And the actual test is unlikely to be as poorly-written as this question seems to be. As John just pointed out, all answers to this question are fundamentally broken. There is no correct answer here. I would add that even if we removed the super.hashCode() from each option, answer 2 would not be a particularly good answer - answers 3 or 4 would be better. Ordinarily I'd say 4 is better since it uses both name and age, both of which are properties of the class. Except that in the real world, age can change over time, so it isn't really a good idea to have this as a fundamental property of the class. Oh well.

Now, if this had been a question from the actual test, someone probably would have deleted it. They may still do so. I suggest reading the forum's Position on Real Questions - Must Read!. This is not a good place to ask about real exam questions. Or even badly-misquoted versions of real exam questions.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic