Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2006

Tip: Looking for answers? Try searching our database.

hashcode

Thread view: 
gk - 15 Sep 2006 15:35 GMT
public int hashCode()

   Returns a hash code for this string. The hash code for a String
object is computed as

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

How this value is calculated ?

say i have  String s="abc"

so ,

s[0]= a
s[1]=b
s[2]=c

but how do i get the power of a character ?

say the first term ...
a*31^2   = whats this ? whats the neumerial value of it ?
Hendrik Maryns - 15 Sep 2006 15:54 GMT
gk schreef:
> public int hashCode()
>
[quoted text clipped - 17 lines]
> say the first term ...
> a*31^2   = whats this ? whats the neumerial value of it ?

chars are converted to ints if arithmetic is done with them.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Patricia Shanahan - 15 Sep 2006 16:42 GMT
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 18 lines]
>>
>> but how do i get the power of a character ?

The calculation does not call for any powers of characters, only powers
of 31. Exponentiation, which is what is meant in this context by "^",
has higher precedence than multiplication.

>> say the first term ...
>> a*31^2   = whats this ? whats the neumerial value of it ?
>
> chars are converted to ints if arithmetic is done with them.

Also, it is not normal, or efficient, to evaluate a polynomial by
explicitly calculating e.g. x^(n-1) etc.

Usually, it is done by alternately adding a coefficient, starting with
the one for the highest power, and then multiplying by x (in this case,
by 31).

So start with s[0]. While the last character to have been used is not
the last character of the string, multiply the sum so far by 31 and add
the next character.

Patricia
Daniel Dyer - 15 Sep 2006 17:04 GMT
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
[quoted text clipped - 21 lines]
> of 31. Exponentiation, which is what is meant in this context by "^",
> has higher precedence than multiplication.

This isn't Pascal, so that's XOR, which has a lower precedence than  
multiplication.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Patricia Shanahan - 15 Sep 2006 17:51 GMT
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
[quoted text clipped - 24 lines]
> This isn't Pascal, so that's XOR, which has a lower precedence than
> multiplication.

You seem to have been confused by missing context. The quote is neither
Pascal, nor Java, nor any other programming language, but a mathematical
expression represented as text in the String hashCode documentation. In
context:

"Returns a hash code for this string. The hash code for a String object
is computed as

    s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

using int arithmetic, where s[i] is the ith character of the string, n
is the length of the string, and ^ indicates exponentiation. (The hash
value of the empty string is zero.)"

[http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#hashCode()]

Patricia
Daniel Dyer - 15 Sep 2006 19:34 GMT
>>> The calculation does not call for any powers of characters, only powers
>>> of 31. Exponentiation, which is what is meant in this context by "^",
[quoted text clipped - 18 lines]
>
> [http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#hashCode()]

Oops, sorry.  I've never thought to look at how String implements hashCode.

Dan.

Signature

Daniel Dyer
http://www.dandyer.co.uk



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.