Java hashCode() 方法

Java 教程 · 2019-02-08 21:57:53

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

以上程序执行结果为:

字符串的哈希码为 :321005537

点击查看所有 Java 教程 文章: https://codercto.com/courses/l/12.html

查看所有标签

Learn Python the Hard Way

Learn Python the Hard Way

Zed A. Shaw / Addison-Wesley Professional / 2013-10-11 / USD 39.99

Master Python and become a programmer-even if you never thought you could! This breakthrough book and CD can help practically anyone get started in programming. It's called "The Hard Way," but it's re......一起来看看 《Learn Python the Hard Way》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码