獲取 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