Java 教程 Java hashCode() 方法

vernon · 2022-05-27 00:00:07 · 热度: 9

hashCode() 方法用于返回字符串的哈希码。

字符串对象的哈希码根据以下公式计算:

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]

使用 int 算法,这里 s[i] 是字符串的第 i 个字符的 ASCII 码,n 是字符串的长度,^ 表示求幂。空字符串的哈希值为 0。

语法

public int hashCode()

参数

  • 无。

返回值

返回对象的哈希码值。

实例

实例

public class Test {
        public static void main(String args[]) {
                String Str = new String("www.codercto.com");
                System.out.println("字符串的哈希码为 :" + Str.hashCode() );
        }
}

以上程序执行结果为:

字符串的哈希码为 :321005537

查看更多 Java String 类 相关内容

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册