Java - Boolean Class hashCode() Method

Boolean class hashCode() method: Here, we are going to learn about the hashCode() method of Boolean class with its syntax and example. By Preeti Jain Last updated : March 17, 2024

Boolean class hashCode() method

  • hashCode() method is available in java.lang package.
  • hashCode() method is used to return hashcode of the Boolean object.
  • hashCode() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • hashCode() method does not throw an exception at the time of returning hash code.

Syntax

public int hashCode();

Parameters

  • It does not accept any parameter.

Return Value

The return type of this method is int - it returns hash code for this Boolean object.

Note:

  • If the given Boolean object value is true then the hashcode of this value 1231.
  • If the given Boolean object value is false then the hashcode of this value 1237.

Example

// Java program to demonstrate the example 
// of int hashCode() method of Boolean class

public class HashCodeOfBooleanClass {
    public static void main(String[] args) {
        // Variables initialization
        boolean value1 = true;
        boolean value2 = false;

        // It returns hashcode value denoted by this Boolean b1 object
        // by calling b1.hashCode()
        Boolean b1 = new Boolean(value1);

        // Display b1 result
        System.out.println("b1.hashCode(): " + b1.hashCode());

        // It returns hashcode value denoted by this Boolean b2 object
        // by calling b2.hashCode()
        Boolean b2 = new Boolean(value2);

        // Display b2 result
        System.out.println("b2.hashCode(): " + b2.hashCode());
    }
}

Output

b1.hashCode(): 1231
b2.hashCode(): 1237

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.