beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Boolean.hashCode() Method in Java

By Chaitanya Singh | Filed Under: java.lang.Boolean

Description

Program to get hash code for Boolean object. We are using hashCode() method of Boolean class for this purpose, it returns integer 1231 if this object represents true; returns the integer 1237 if this object represents false.

Example

class BooleanHashCodeEx {

   public static void main(String[] args) {

      // Boolean objects
      Boolean bObj, bObj2;

      // Assigning values to both the objects
      bObj = new Boolean(true);
      bObj2 = new Boolean(false);

      // integers to store hash code
      int icode, icode2;

      // Getting Hash Code
      icode = bObj.hashCode();
      icode2 = bObj2.hashCode();

      // Displaying HashCode for both the objects
      System.out.println("Hash code of "+bObj+" is "+icode);
      System.out.println("Hash code of "+bObj2+" is "+icode2);
   }
}

Output:

Hash code of true is 1231
Hash code of false is 1237

public int hashCode(): Returns a hash code for this Boolean object. It returns the integer 1231 if this object represents true; returns the integer 1237 if this object represents false.

Enjoyed this post? Try these related posts

  1. Compare boolean values in java – compareTo() Method
  2. Convert String Object to Boolean Object in Java
  3. Cast Boolean Object to boolean in Java – booleanValue() Method
  4. Convert String to boolean primitive in Java: parseBoolean() method
  5. Convert boolean Primitive to Boolean object in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap