hashcode

G

gk

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 ?
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

gk schreef:
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 ?

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFFCr6Te+7xMGD3itQRAsa1AJsFRAnoc7o/6FjxpFd9FqWWVVeI8ACePsom
i2NL19s7Sbar2yLAW51GA/M=
=/Thm
-----END PGP SIGNATURE-----
 
P

Patricia Shanahan

Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

gk schreef:
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 ?

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.
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
 
D

Daniel Dyer

Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
gk schreef:
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 ?

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.

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

Dan.
 
P

Patricia Shanahan

Daniel said:
Hendrik said:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
gk schreef:
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 ?

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.

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 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
 
D

Daniel Dyer

Daniel said:
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 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()]


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

Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top