This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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: 3473
  • 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?
 
Fire me boy! Cool, soothing, shameless self promotion:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic