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

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

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

Boolean 类的hashCode()方法是一个内置方法,用于返回对应于 Boolean 对象的int hashcode 值。

句法

BooleanObject.hashCode()

返回类型:它返回一个与布尔对象对应的int hashcode 值。如果布尔对象存储值true ,则返回1231 。如果布尔对象存储值false ,则返回1237

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

方案一:

// java code to demonstrate
// Boolean hashCode() method
  
class GFG {
    public static void main(String[] args)
    {
  
        // creating Boolean object
        Boolean b = new Boolean(true);
  
        // hashCode method of Boolean class
        int output = b.hashCode();
  
        // printing the output
        System.out.println(output);
    }
}
输出:
1231

方案二:

// java code to demonstrate 
// Boolean hashCode() method
  
class GFG {
    public static void main(String[] args)
    {
  
        // creating Boolean object
        Boolean b = new Boolean(false);
  
        // hashCode method of Boolean class
        int output = b.hashCode();
  
        // printing the output
        System.out.println(output);
    }
}
输出:
1237