This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes hashCode() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashCode()" Watch "hashCode()" New topic
Author

hashCode()

Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
Could anyone say about hashCode() method and y we need to override it ??
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Did you SearchFirst ? You'll find plenty of information about that method with a simple search. And UseRealWords (y -> why).


[My Blog]
All roads lead to JavaRanch
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
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
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arun C. Giridharan
Ranch Hand

Joined: Jul 11, 2010
Posts: 96
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
Sheriff

Joined: Sep 28, 2004
Posts: 18492
    
  40

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: Nov 04, 2009
Posts: 2066

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.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hashCode()
 
Similar Threads
hash code
Practice Question
basicjava
need help with the space-time continium
what is hashcode and equals contract?