谁能告诉我 Java String.hashCode()
方法可以返回的最大和最小 int
值是多少?
最佳答案
String.hashCode ()
返回一个int
,使用下面的公式计算:
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]
using int arithmetic, where
s[i]
is the i-th character of the string, n is the length of the string, and ^ indicates exponentiation. (The hash value of the empty string is zero.)
可以使用以下常量找到最小值和最大值。
System.out.println (java.lang.Integer.MAX_VALUE); // 2147483647
System.out.println (java.lang.Integer.MIN_VALUE); // -2147483648
关于Java hashCode方法最大返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11480009/