aspose file tools*
The moose likes Java in General and the fly likes Again abt Hashing..? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Again abt Hashing..?" Watch "Again abt Hashing..?" New topic
Author

Again abt Hashing..?

Prashant Neginahal
Ranch Hand

Joined: Dec 04, 2002
Posts: 76
Hi All,
I had read that hashing is means of finding the location by some key information of object.
That is
String str="Hello";
str.hashcode() --> gives the address where it should be stored(or where it is stored rather )

First question ,is it right?
if yes,then
String str1=new String("Hello");
String str2=new String("Hello");
why should str1 and str2 give same hashcode, when these two are stored in two different locations?
Pls someone clearly explain me about this hashing implementation in Java.


--------------<br />Prashant<br />SCJP-91%
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The hashcode method as implemented by the class Object returns the memory address of the object (if I recall correctly).
This hashcode method is overridden in the String class and it behaves differently. The hashcode method of the String class returns an int calculated from the value of the String object. So, two different String objects with the same value (where String1.equals(String2) is true) should calculate to the same hashcode.
Take a look at the October issue of The JavaRanch Newsletter for more relevant information and take a look at Chapter 12, Section 3 of David J. Eck's Introduction to Programming Using Java for a decent, short introduction to Mapping data structures and how the hashcode can be useful.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
If for some reason you really wanted the behavior of the hashcode method as implemented in the Object class, then you could use System.identityHashCode(Object).
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

Originally posted by Dirk Schreckmann:

This hashcode method is overridden in the String class and it behaves differently. The hashcode method of the String class returns an int calculated from the value of the String object. So, two different String objects with the same value (where String1.equals(String2) is true) should calculate to the same hashcode.

To spoil the punchline, the hashcode of a Java object is intimately tied to its equality, and could (I suppose) also be used to characterize it's 'comparability' to objects of the same type.
We say two object references are equal if they point to the same object. So a reasonable default implementation of hashCode() would be to encapsulate this self-evident rule by returning addresses.
In the case of String objects, we want equality to be more broadly defined as two objects that have the same content, even if they are in different memory locations. Overriding hashCode() to return a value a "unique" content-hash thus supports tests of equality.
Here's a little play code that might help you consider how the String hash is derived. It's nothing fancy, but I hope enouh to provoke some thoughts:

Run it; see whatcha get.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Dirk Schreckmann:
The hashcode method as implemented by the class Object returns the memory address of the object (if I recall correctly).

This isn't guaranteed behaviour:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Ilja Preuss:
This isn't guaranteed behaviour
In particular, on CPU architectures with an address space larger than 32 bits (4GB), a Java integer simply won't accomodate the full address.
- Peter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Again abt Hashing..?