• Post Reply Bookmark Topic Watch Topic
  • New Topic

How HashCode value is calculated for objects  RSS feed

 
Sudhakar Reddy Kurakula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to know how the implemention for calculating the hashcodes for objects is provided in Object class.

In the same manner for equals() method also.

I came to know that HashCode() and equals() mehthods have been implemented in string class.

what technique is used in both classes?

can anybody explain me?

Cheers
Sudha
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The source code for all the java.* API classes is included in the JDK distribution, in a file named "src.jar" or "src.zip". It's often useful to look at that source to answer questions like these.

For Object, hashCode() is a native method in Sun's JDKs. The value is derived from some native characteristic of the object -- for example, its address in memory. Notice I said "derived from" -- the hashCode is not itself the object's address in RAM, but it may be related to it.

Object.equals() just uses the "==" operator.

String overrides both these methods. String.hashCode() computes a value using all the characters by adding, shifting, multiplying, and operations of this sort. The equation is given on the Javadoc page for the String class. String.equals() compares all the characters of the two strings to see that they're the same.
[ December 31, 2006: Message edited by: Ernest Friedman-Hill ]
 
Sudhakar Reddy Kurakula
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for giving answer.

Cheers
Sudha
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!