Results 1 to 18 of 18
  1. #1
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default HashCode value of String class Object!

    The hashCode() function of a String Class Returning the Same hashcode value for the equal strings.I think hashCode is the Decimal representation of Objects Hexadecimal memory address.But,if i compare the two equal strings with if statement as follows
    if(s1==s2) //say s1=new String("Hello");s2=new String("Hello");
    It returns the false;because the two Strings are at two different memory locations.
    My Question:-
    ------------
    How the String Class is Calculating the "hash-code" for its Objects?How it is printing the same "hash-code" value for two equal strings,though both are at two different memory locations?Is it not calculating the hash-code value based on the Objects memory address?If so,What exactly the magic behind returning the same "hash-code" value for two equal strings even though both are at two different locations?Its urgent please reply it.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Find the String class in the src.zip file contained within the JDK directory and look at the code. You have no real reason to know how the hascode is calculated, but you do need to know that the hashcode value is predicated upon the value of the String (i.e. the letters h e l l o), not on the String's reference value (call it memory address if you will).

  3. #3
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    First of all thank you for your reply.
    Yeah!That might be the way of calculating the hash-code value for the Strings.
    Generally,for Objects of normal classes(I mean,User-defined classes) the hash-code Value Returned by the hashCode() function of the 'Object' class(Great Grand Parent for all Java Classes) is a Decimal Representation of Hexadecimal memory location.That's why I think,It's also being true for the String class also.But,you r right.The String class Overrides the hashCode() function of 'Object' class;hence it might have change the way of calculating the "hash-code" value as you said earlier;based on characters of a String.So we are getting the same hash-code value for two equal strings.I got it!ThanQ!
    But,I wanna know how exactly it is calculating the hash-code?please let me know it.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    As I said, see the source code. There is no reason for this, however.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Also, there is nothing in the language specification that says that the value returned by hashcode() for an Object is the memory address (or some representation of). It just happens to be the way (currently, could change) that the Sun JRE (at least) does it.

  6. #6
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    No,Mr.Tolls,In src.zip folder under <java-install-path>/JDK<version>.Just go through "Object.java" class,There,at hashCode() function Under comments they mentioned that the hash-code returned by the hashCode() function is the Integer representation of the objects internal memory address.
    Anyhow,for Strings they override the hashCode() function and calculated the hash-code based on characters of a string as follows...
    h=0;
    for(int i=0;i<len;i++) //len is the length of the string
    {
    h=31*h+val[off++];
    //val is a character array which holds the characters of a current String
    //object
    }
    there might be some logic for why they have chosen "31" there.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Because it's a prime number, could just as easily have been 7 or 11 or ....

  8. #8
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    yeah!ThanQ!For your Information Mr.Misijuade.But still,why only prime number 31 why cant it be any other prime like 17,19,23 etc.. [or] any prime greater than 31?

  9. #9
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Sorry Masijade , I couldn't pronounce your name properly.Anyhow,execuse me!

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by Anjaneyulu View Post
    yeah!ThanQ!For your Information Mr.Misijuade.But still,why only prime number 31 why cant it be any other prime like 17,19,23 etc.. [or] any prime greater than 31?
    Like I said, it could just as easily have been any other.

  11. #11
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Thumbs up

    Ok!I got it,ThanQ!

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    Quote Originally Posted by Anjaneyulu View Post
    No,Mr.Tolls,In src.zip folder under <java-install-path>/JDK<version>.Just go through "Object.java" class,There,at hashCode() function Under comments they mentioned that the hash-code returned by the hashCode() function is the Integer representation of the objects internal memory address.
    Look at the end of the Javadoc for hashcode()...

    "This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java<font size="-2"><sup>TM</sup></font> programming language."

    Which is what I said. It's not specified how. The only requirements of hashcode are the three bits of the contract specified near the top of the javadoc.

  13. #13
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    As of I understood,They didn't provide the implementation code for hashCode() function in "Object.java" in hashCode docs(may be they were hidden in the documentation). But really there should be the code for hashCode() function(ofcourse hiding in docs).Otherwise you cant get the hash-code value for the objects upon calling the hashCode() function by the objects.The last line in docs which you posted to refer by me doesn't mean hshCode() function shouldn't give the integer representation of objects internal memory address.It internally does this job;it always gives the hash-code value as an integer representation of object address only unless it is overriden and change the code just as did in String class.The documentation hides how it'll convert the Integer from hexadecimal that's why they have written that comment there as of i understood!

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default

    They don't provide the code because, in the Sun version of Object, it is native code, not Java.

    No, they wrote the comment because, as said, someone writing their own JDK is not required to do it the same way that Sun did. The only requirement of hashCode is stated earlier in the javadoc for the method. Also, most classes should not use the default hashCode() method...

  15. #15
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    Yeah!You are Right Mr.Tolls.ThanQ for the info...

  16. #16
    gionkunz is offline Member
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Default

    Hi, with prime numbers you can avoid hashCode collisions. They have choosen 31 because it's a low number and it gets converted to a bitshift operation when the bytecode is generated which makes the multiplication much faster... (31 * x is like x << 5 )

  17. #17
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

    Default

    ThanQ Mr.jiankunz.It Means one left shift results in multiplcation of x by 2.Here,5 times left shift means multiply by 32.To get 31*x.
    (x<<5)/5 right it is an easy calculation for the processor.Good!

  18. #18
    Anjaneyulu is offline Member
    Join Date
    Feb 2010
    Posts
    29
    Rep Power
    0

Similar Threads

  1. hashCode questions
    By alpdog14 in forum New To Java
    Replies: 3
    Last Post: 02-19-2010, 11:00 PM
  2. An acceptable hashcode?
    By dsym@comcast.net in forum New To Java
    Replies: 7
    Last Post: 03-29-2009, 09:04 PM
  3. [SOLVED] ReadLine(String fmt,Object... args) of Console class
    By Pooja Deshpande in forum New To Java
    Replies: 4
    Last Post: 04-25-2008, 05:51 AM
  4. Replies: 1
    Last Post: 04-09-2008, 02:15 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •