Java hashCode() 方法
hashCode() 方法用於返回字符串的哈希碼。
字符串對象的哈希碼根據以下公式計算:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
使用int 算法,這裡s[i] 是字符串的第i 個字符,n 是字符串的長度,^ 表示求冪。 空字符串的哈希值為0。
語法
public int hashCode()
參數
- 無。
返回值
返回對象的哈希碼值。
實例
public class Test { public static void main(String args[]) { String Str = new String("www.w3big.com"); System.out.println("字符串的哈希码为 :" + Str.hashCode() ); } }
以上程序執行結果為:
字符串的哈希码为 :321005537