获取 Java 中字符串的哈希码
hashCode() 方法用于获取字符串的哈希码。此方法不接受任何参数,因为它是一个默认方法,且会返回一个哈希码值。
下面给出了一个演示 Java 中 hashCode() 方法的程序
示例
import java.io.*;
public class Demo {
public static void main(String args[]) {
String str = new String("The sky is blue");
System.out.println("The string is: " + str);
System.out.println("The Hashcode for the string is: " + str.hashCode());
}
}输出
The string is: The sky is blue The Hashcode for the string is: -729257918
现在让我们来理解一下上面的程序。
定义字符串 str。然后打印字符串,并使用 hashCode() 方法打印其哈希码。以下代码段演示了此过程:
String str = new String("The sky is blue");
System.out.println("The string is: " + str);
System.out.println("The Hashcode for the string is: " + str.hashCode());
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP