Java String hashCode()
方法返回此字符串的哈希码。 String
对象的哈希码计算如下 -
s[0]*31^(n - 1) + s[1]*31^(n - 2) + ... + s[n - 1]
使用整数算法,其中s[i]
是字符串的第i
个字符,n
是字符串的长度,^
表示取幂(空字符串的哈希值为零。)
语法
以下是此方法的语法 -
public int hashCode()
参数
- N/A
返回值
- 返回此对象的哈希码值。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str = new String("Welcome to xuhuhu.com");
System.out.println("Hashcode for Str :" + Str.hashCode());
}
}
执行上面示例代码,得到以下结果:
Hashcode for Str :1906679467
上一篇:
Java String类
下一篇:
Java快速入门