• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inquisition Mock - Doubt

 
Shivani Sharma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

In the following code :



Ans given is true.

Shouldn't it be false as hashcode is not overidden.
 
Sebastian Janisch
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No ... hashCode becomes important when working with collections to determine lookup locations for objects.

In collections, the hashCode determines the location to look for the object, then equals determines equality. Which also means two identical hashCode do not necessarily mean equality. The opposite however is true. Two equal objects must have the same hashCode.
 
Harsh Pensi
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Shivani
I failed to understand why you need hashcode here
hashcode method has to be overridden if you want to be able to use the class properly in a collection that uses hashing.
 
Deepak Bala
Bartender
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivani Sharma wrote:Hi Guys

In the following code :



Ans given is true.

Shouldn't it be false as hashcode is not overidden.


There is no underlying structure that tries to put these objects into "buckets". Try this

 
Shivani Sharma
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot guys, got it.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic