Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

what is hashcode and equals contract?

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is hashcode and equals contract?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is described in the API, under java.lang.Object's hashCode() method.
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi...
I understand that:

equals() using to comparison, refers to equivalence relations.
hashCode() is id number allocated to an object by JVM.
Contract: When you use equals() and hashCode() methods is necessary to override these methods

But checking please:
https://coderanch.com/t/269570/java-programmer-SCJP/certification/Let-discuss-hashcode-contract

http://www.javaworld.com/community/?q=node/1006
http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf
 
Author
Posts: 3461
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The key thing to remember is that:

-- If two objects are equal then they should have the same hascode but the reverse is not true (i.e. If two objects have the same hashcode does not mean that they are equal)

-- You need to use the same attributes in your equals & hashCode methods


Look at the doco for further info.
 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides checking about the implications of equals and hashcode values, the exam may simply ask that if you implement equals() method, which other method you should implement?
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
The trailboss has a kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic