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

Understanding hashcode()

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from enthuware software:-



Which of the following options would satisfy the equals and hashCode contract at //1?

Option 1: return 0;
Option 2: return a;;
Option 3: return a+b;
Option 4:return a*a;
Option 5: return a/2;

Please help me understanding the hashcode as in this problem it is not clear to me what will be the output and why?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The equals method must be Reflexive, Symmetric, Transitive and Consistent. That means:

Reflexive: x.equals(x) must be true
Symmetric: if x.equals(y) is true , then y.equals(x) must be true
Transitive: if x.equals(y) is true and x.equals(z) is true, then y.equals(z) must be true
Consistent: multiple calls to equals must return in the same result

Now as for hashCode(), there is one major rule:
If two objects are equal, the hashcode needs to be equal as well. The same however does not apply in reverse.

Given the information above you should be able to solve the question.
 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sebastian ,

I could not understand what are you trying to say on the hashcode???
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If two objects are equal (x.equals(y) == true), then x.hashCode() and y.hashCode() must return the same value.

However, if x.equals(y) == false, then x.hashCode() and y.hashCode() may return the same value.

It's as simple as that.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

A very good explanation on hashcodes can be found in "SCJP Sun Certified Programmer for Java 6 Study Guide (Exam 310-065)" by Katherine Sierra, Bert Bates.
Here is a direct link on the subject: http://books.google.com/books?id=Eh5NcvegzMkC&pg=RA1-PA550&lpg=RA1-PA550&dq=hashCode+buckets&source=bl&ots=ESMaHJV2Lo&sig=-WqKNWtannh0sBwugUtirYXJXp8&hl=en&ei=xBWBSve8HdeG_Aa7lYy5Cw&sa=X&oi=book_result&ct=result&resnum=6#v=onepage&q=hashCode%20buckets&f=false
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch Narcisa
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the answer offered Multiple choice?

 
Sonali Sehgal
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ Narcisa
I am already studying from Kathy Sierra

@Stephen yes the question is answered multiple choice and I already know the answer
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Leaving option 3 all others are appropriate. only the contract that needed to be checked is equals = true(definitely same hashcodes) and hashcode != true(definitely not equal).
 
Bras cause cancer. And tiny ads:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic