Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is hashcode and equals contract?

 
varinder mahajan
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is hashcode and equals contract?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is described in the API, under java.lang.Object's hashCode() method.
 
Mario Razec
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • 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:
http://www.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
 
arulk pillai
Author
Ranch Hand
Posts: 3388
  • Mark post as helpful
  • send pies
  • 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.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic