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快速入門