Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Again abt Hashing..?

 
Prashant Neginahal
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic