Home » Java programming language

Java Boolean class hashCode() method with example

Boolean class hashCode() method: Here, we are going to learn about the hashCode() method of Boolean class with its syntax and example.
Submitted by Preeti Jain, on October 25, 2019

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();

Parameter(s):

  • 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

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.