Java Byte hashCode()方法及示例

来源:这里教程网 时间:2026-02-17 20:39:57 作者:

Java Byte hashCode()方法及示例

Byte类的 hashCode() 方法是Java中的一个内置方法,用于返回ByteObject的哈希代码。

注意:hashCode()的返回值与intValue()相同。

语法

ByteObject.hashCode()

返回值: 它返回一个 int 值,代表指定字节对象的哈希代码。

下面是hashCode()方法在Java中的实现。

例1 :

// Java code to demonstrate// Byte hashCode() method  class GFG {    public static void main(String[] args)    {          String value = "74";          // wrapping the byte value        // in the wrapper class Byte        Byte b = new Byte(value);          // hashCode of the Byte Object        int output = b.hashCode();          // printing the output        System.out.println("HashCode of "                           + b + " : " + output);    }}

输出:

HashCode of 74 : 74

例2 :

// Java code to demonstrate// Byte hashCode() method  class GFG {    public static void main(String[] args)    {          String value = "-17";          // wrapping the byte value        // in the wrapper class Byte        Byte b = new Byte(value);          // hashCode of the Byte Object        int output = b.hashCode();          // printing the output        System.out.println("HashCode of "                           + b + " : " + output);    }}

输出:

HashCode of -17 : -17

相关推荐