Java.lang Package Classes

Java Boolean - hashCode() Method



The java.lang.Boolean.hashCode() method returns a hash code for a boolean value which is compatible with Boolean.hashCode().

Syntax

public static int hashCode(boolean value)

Parameters

value Specify the value to hash.

Return Value

Returns the integer 1231 if this object represents true; returns the integer 1237 if this object represents false.

Exception

NA.

Example:

In the example below, the java.lang.Boolean.hashCode() method returns a hash code for the given boolean value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating boolean values
    boolean b1 = true;
    boolean b2 = false;

    //printing hashcode of the boolean values
    System.out.println("hashCode of the b1 is: " + Boolean.hashCode(b1)); 
    System.out.println("hashCode of the b2 is: " + Boolean.hashCode(b2));   
  }
}

The output of the above code will be:

hashCode of the b1 is: 1231
hashCode of the b2 is: 1237

❮ Java.lang - Boolean