• 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

What exactly is hashcode?

 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that hashcode is a unique number that identifies objects, but it can't be like a memory address cause 2 different objects can have the same hashcode, right? I'm confused about what this hashcode is exactly. How is it calculated? And what is it used for?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is a native method in the Object class. Therefore it may be calculated differently based on the OS. It is not a memory address. Have you seen http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html ?
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I looked through the API and also read about hashcodes in many different resources, but still never fully understood how they were calculated. I know they're used for the equals() method, but how exactly are they calculated? And are they small ints, like 1 or 2, or big like 214563 or something? And do we ever need to know the exact value of the hashcode? Or can we just rely that they are there and if 2 objects are "equals" then their hashcodes will be the same? And I'm not that familiar with hashtables, never used them. I'll read up more on those, maybe that will clear up some of the cobwebs for me.
Thanks Marilyn!

------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud
 
Marilyn de Queiroz
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I learned about hashcodes by reading Data Structures and Algorithms in Java by Mitchell Waite. Although I'm sure there are other resources about them, I haven't persued it further.
[This message has been edited by Marilyn deQueiroz (edited November 04, 2001).]
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anyone can write a hashcode algorithm. As a matter of fact, if you are writing a class, and you decide to override the equals() method of Object, then it is STRONGLY recommended that you create an appropriate hashcode() to go with it. This is so that you preserve the contract of .equals that says that if two things are equal - they will return the same hashcode. The .equals of Object just looks to see if they both point to the SAME instance.
Remember that the reason that you want one of these things to begin with is to create some sort of ordering mechanism to increase performance when dealing with things like hashTables and such.
The only rules are:
1. It must return an int value.
2. If two instances of the class are .equal() then they MUST return the same hashcode. Two unequal instances are allowed to return the same hashcode.

You COULD have a hashcode method that always returns the same integer. It would be LEGAL (although not too intelligent).
So the next consideration is - how complicated do you want this hashcode algorithm to be. If you make it too simpilistic (like above) then too many instances will result in the same hashcode and you wont get any sorting or searching benefit out of it. On the other hand if you make it too complicated the benefit gained by having unique hashcodes gets eaten up by the resources required to calculate the darn thing to begin with. Therefore you need to find some middle ground. Somewhere where MOST of the instances are going to have unique hashcodes, but if the occasional case happens and they are not unique it will not slow down performance much.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Although this method is implemented in class Object, it is recommended that you override this method and provide your own implementation for classes you write. Just as it is a good idea to provide your own overriding 'equals()' and 'toString()' methods.
How you implement hashCode() is completely up to you as long as it satisfies the hashCode() contract - you can see this in the java docs API for the Object class.
So for example if you are writing a Time class that holds hours (HH), minutes (MM) and seconds (SS) then your implementation of hashCode() might return the integer represented by HHMMSS or you might decide to return the integer representing the number of seconds past midnight that the Time object represents. It's your choice.
Or in a 'Person' object you might concatenate the Surname with Forename and use String's hashCode() method to get the integer to return.
So as to the size of the integer - it can be whatever you want it to be.
One thing to bear in mind though is that the way you compute hashcodes could have an influence on the performance of hashtables, hashmaps etc. as it would increase look up times if many unequal objects had the same hashcode.
 
Ranch Hand
Posts: 782
Python Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason Bloch's "Effective Java" provides an algorithm for
writing hashCode() method. Look it up.
Pho
 
reply
    Bookmark Topic Watch Topic
  • New Topic