Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java String hashCode algorithm

807589Sep 21 2008 — edited Sep 21 2008
I was playing around with hash codes and I noticed that some hash codes for a String object is negative. While not a revelation in itself, I'm curious about the mechanism that causes it. The javadoc states that the hashcode is computed via the algorithm
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
As far as I can tell, this shouldn't yield a negative number. Is some kind of wrapping around taking place?

Thoughts? comments?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 19 2008
Added on Sep 21 2008
6 comments
667 views