📜  Java中的双 hashCode() 方法及示例

📅  最后修改于: 2022-05-13 01:55:38.893000             🧑  作者: Mango

Java中的双 hashCode() 方法及示例

Double 类的hashCode()方法是用于返回此 Double 对象的 hashcode 值的内置方法。

句法:

DoubleObject.hashCode()

参数:不带参数。

返回类型:它返回一个int值。返回的值是(int)(v^(v>>>32))其中 v 是一个 long 变量,等于Double.doubleToLongBits(this.doubleValue())

下面是 hashCode() 方法的实现:

示例 1:

// Java code to demonstrate
// Double hashCode() Method
  
class GFG {
    public static void main(String[] args)
    {
  
        double d = 118.698;
  
        // creating Double object.
        Double value = new Double(d);
  
        // hashCode() method Double class
        int output = value.hashCode();
  
        // printing the output
        System.out.println("Hashcode Value of "
                           + value + " : "
                           + output);
    }
}
输出:
Hashcode Value of 118.698 : 1215072837

示例 2:

// Java code to demonstrate
// Double hashCode() Method
  
class GFG {
    public static void main(String[] args)
    {
  
        int i = -30;
  
        // creating Double object.
        Double value = new Double(i);
  
        // hashCode() method Double class
        int output = value.hashCode();
  
        // printing the output
        System.out.println("Hashcode Value of "
                           + value + " : "
                           + output);
    }
}
输出:
Hashcode Value of -30.0 : -1069678592