Java - Byte hashCode() 方法



描述

Java Byte hashCode() 方法返回此 Byte 的雜湊碼,它等於呼叫 intValue() 的結果。

宣告

以下是 java.lang.Byte.hashCode() 方法的宣告

public int hashCode()

重寫

Object 類中的 hashCode

引數

返回值

此方法返回此 Byte 的雜湊碼值。

異常

使用 new 運算子建立的 Byte 物件的獲取 HashCode 示例

以下示例演示了使用 new 運算子建立的 Byte 物件的 Byte hashCode() 方法的使用。我們建立兩個 Byte 變數,併為它們分配使用 new 運算子建立的 Byte 物件。然後建立 int 變數,並使用 hashCode() 方法為其分配 byte 變數的雜湊碼,然後列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // create 2 int i1, i2
      int i1, i2;

      // assign values to b1, b2
      b1 = new Byte("100");
      b2 = new Byte("-100");

      // assign hash code of b1, b2 to i1, i2
      i1 = b1.hashCode();
      i2 = b2.hashCode();

      String str1 = "Hashcode of Byte " + b1 + " is " + i1;
      String str2 = "Hashcode of Byte " + b2 + " is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hashcode of Byte 100 is 100
Hashcode of Byte -100 is -100

使用 valueOf(String) 方法建立的 Byte 物件的獲取 HashCode 示例

以下示例演示了使用 valueOf(String) 方法建立的 Byte 物件的 Byte hashCode() 方法的使用。我們建立兩個 Byte 變數,併為它們分配使用 valueOf(String) 方法建立的 Byte 物件。然後建立 int 變數,並使用 hashCode() 方法為其分配 byte 變數的雜湊碼,然後列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // create 2 int i1, i2
      int i1, i2;

      // assign values to b1, b2
      b1 = Byte.valueOf("100");
      b2 = Byte.valueOf("-100");

      // assign hash code of b1, b2 to i1, i2
      i1 = b1.hashCode();
      i2 = b2.hashCode();

      String str1 = "Hashcode of Byte " + b1 + " is " + i1;
      String str2 = "Hashcode of Byte " + b2 + " is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hashcode of Byte 100 is 100
Hashcode of Byte -100 is -100

使用 valueOf(byte) 方法建立的 Byte 物件的獲取 HashCode 示例

以下示例演示了使用 valueOf(byte) 方法建立的 Byte 物件的 Byte hashCode() 方法的使用。我們建立兩個 Byte 變數,併為它們分配使用 valueOf(byte) 方法建立的 Byte 物件。然後建立 int 變數,並使用 hashCode() 方法為其分配 byte 變數的雜湊碼,然後列印結果。

package com.tutorialspoint;

public class ByteDemo {
   public static void main(String[] args) {

      // create 2 Byte objects b1, b2
      Byte b1, b2;

      // create 2 int i1, i2
      int i1, i2;

      // assign values to b1, b2
      b1 = Byte.valueOf((byte)100);
      b2 = Byte.valueOf((byte)-100);

      // assign hash code of b1, b2 to i1, i2
      i1 = b1.hashCode();
      i2 = b2.hashCode();

      String str1 = "Hashcode of Byte " + b1 + " is " + i1;
      String str2 = "Hashcode of Byte " + b2 + " is " + i2;

      // print i1, i2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Hashcode of Byte 100 is 100
Hashcode of Byte -100 is -100
java_lang_byte.htm
廣告