Java字符串:hashCode() 方法

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("sunny chen");
        System.out.println("字符串的哈希码为 :" + Str.hashCode() );
    }
}

版权声明:本文为Mrs_chens原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。