• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode()

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could anyone say about hashCode() method and y we need to override it ??
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic