Java Short hashCode()方法及示例

来源:这里教程网 时间:2026-02-17 21:09:49 作者:

Java Short hashCode()方法及示例

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

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

语法

ShortObject.hashCode()

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

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

例1 :

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

输出:

HashCode of 74 : 74

例2 :

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

输出:

HashCode of -17 : -17

相关推荐