Skip to main content
2 of 2
formatting
Raptor
  • 54.4k
  • 47
  • 254
  • 405

String.hashcode() Clarification

I was looking at the String.hashcode() function. It's defined as:

This method 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.)

The question here is s[0] refers to the 0th character or rather the nth character in the string. But is it the int value of that character that is used to compute the hashcode?

gkamani2011
  • 223
  • 1
  • 4
  • 14