Java hashCode() 方法

返回到:Java 介绍String类

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

以上程序执行结果为:

字符串的哈希码为 :321005537

返回到:Java 介绍String类

作者:terry,如若转载,请注明出处:https://www.web176.com/java_api/19546.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年5月5日
下一篇 2023年5月5日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注