獲取 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());

更新日期:2019-07-30

213 次瀏覽

開始你的 職業生涯

完成此課程即可獲得認證

開始
廣告
© . All rights reserved.