Skip to main content
formatting
Source Link
Raptor
  • 54.4k
  • 47
  • 254
  • 405

I was looking at the String.hashcode()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]

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

Using int arithmetic, where s[i]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]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?

The question might be a bit stupid, but I am confused.

Thanks in advance :)

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?

The question might be a bit stupid, but I am confused.

Thanks in advance :)

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?

Source Link
gkamani2011
  • 223
  • 1
  • 4
  • 14

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?

The question might be a bit stupid, but I am confused.

Thanks in advance :)