• Post Reply Bookmark Topic Watch Topic
  • New Topic

difference between Equals and HashCode ?  RSS feed

 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

If equals returns true for two objects then hashCode of both the objects are equal. while reverse is not true.

why ?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prabhat Ranjan wrote:
If equals returns true for two objects then hashCode of both the objects are equal. while reverse is not true.

why ?


Think about the reverse situation -- meaning if equals reports false then hash code is different / unique. How many combinations are there? A hash code is an int -- and is limited by the number of unique combinations of an int. An object can be anything -- and have infinite configurations that make them not equal. You would need infinite unique hash code values in order to satisfy the reverse condition.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!