Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

hashCode()

 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could anyone say about hashCode() method and y we need to override it ??
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you SearchFirst ? You'll find plenty of information about that method with a simple search. And UseRealWords (y -> why).
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea ...i searched ....but this code made me SICK




returns 10 and 10 ... Creepy code ... i don't understand how this 2objects can have same Address... returned in Integer Format...
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:
returns 10 and 10 ... Creepy code ... i don't understand how this 2objects can have same Address... returned in Integer Format...


That's because the hashCode() has nothing to do with the current address -- in some cases, the address may have been used, but for most intents and purposes, the hashcode has no relationship with the address.

And in your example, you explicitedly return the value of 10 -- did you think that it wouldn't return what you coded it to return?

Henry
 
Arun C. Giridharan
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because the hashCode() has nothing to do with the current address


The hashCode() must convert the Address into Integer of respective object it searched and return Int ...right.
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:
The hashCode() must convert the Address into Integer of respective object it searched and return Int ...right.


As already mentioned, in this topic, and your other topic.... no.

Henry
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hashCode is the value returned by the hashCode method of the Object class. If you decide to overwrite it, and to return 10, then the hashCode for ALL instances of that class will be the same (10).
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arun Giridharan wrote:
The hashCode() must convert the Address into Integer of respective object it searched and return Int ...right.


Mixing two topics?

OK, hashCode() method is used to differentiate two object with equlas() method. If you don't override the hashCode() method in your class, then the default hashCode() will be inherited from the base Object class, that will bring the memory address of the object. In your class(Well, not exactly ), you override the hashCode() method to give 10 for every object of that class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic