×

Java Boolean hashCode() Method

The hashCode() method of Boolean class returns the hash code for the specified Boolean object or the given Boolean value.

Syntax

  1. public int hashCode()
  2. public int hashCode(boolean value)

Parameters

The parameter ‘value’ represents the value to hash.

Overrides

The hashCode() method overrides hashcode in class object.

Return Value:

The hashCode () method returns a hash code value based on Boolean’s value.

  • It returns 1231, if the Boolean’s value is true.
  • It returns 1237, if the Boolean’s value is false.

Example 1

public class JavaBooleanHashCodeMethodExample1 {
    public static void main(String[] args) {
        Boolean b1 = true;
        //return the hash Code for true
        int val1 = b1.hashCode();
        System.out.println("1. Hash code of true = "+val1);
        Boolean b2 = false;
        int val2 = b2.hashCode();
        //print the hash code
        System.out.println("2. Hash code of false = "+val2);
    }
}

Output

1. Hash code of true = 1231
2. Hash code of false = 1237

Example 2

import java.util.Scanner;
public class JavaBooleanHashCodeMethodExample2 {
    public static void main(String[] args) {
        Scanner scanner= new Scanner(System.in);
        System.out.println("Are you above 18?");
        Boolean b1= scanner.nextBoolean();
        int val=b1.hashCode();
        if(val==1231){
            System.out.println("You are eligible.");
        }
        else{
            System.out.println("Sorry! You are not eligible");
        }
    }
}

Output

Are you above 18?
false
Sorry! You are not eligible

Example 3

public class JavaBooleanHashCodeMethodExample3 {
    public static void main(String[] args) {
        boolean b1 =true;
        //primitive data type's object cannot be used to call Boolean class methods
        int val1 = b1.hashCode();
        System.out.println("Hash code of true  = "+val1);
    }
}

Output

Error:(7, 22) java: boolean cannot be dereferenced

Example 4

public class JavaBooleanHashCodeMethodExample4 {
    static int i=1;
    public static void main(String[] args) {
        int val=Boolean.hashCode(true);
        System.out.println(i+ ". "+val);
        //for any value other than true it will return 1237 or false
        int val1=Boolean.hashCode(Boolean.parseBoolean("abc"));
        System.out.println(++i+ ". "+val1);
    }
}

Output

1. 1231
2. 1237

Related Topics

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 minutes read.

Design of JDBC

Java applications may interface using database systems from many vendors using the Java Database Connectivity (JDBC) Application Software Interface (API) from Sun Microsystem. To connect spreadsheets, JDBC and database drivers...

3 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

Java vs Go

Java: Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say that...

4 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Cast Operator in Java

A cast is a unique operator that completely converts one type of data into another. Casts are unary operators and have the same priority as other unary operators. Type casting in...

3 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Diamond problem in Java

The Diamond Problem in Java is connected to multiple inheritances. It is also referred to as the "deadly diamond dilemma" or even the "deadly diamond of death”. The solution for...

5 minutes read.

How to Reduce Time Complexity in Java

What is time complexity?  The time complexity in java is given as the amount of time a program requires to run or execute it Calculating the time complexity of the program The time...

4 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Java Program to print even and odd numbers using 2 threads

Using two threads in a single thread is even odd printing in Java programming with multiple threads. To create code that prints even and odd using two threads, we must...

2 minutes read.

How to take Array Input in Java

In this tutorial, we will learn about how to take array input in Java. So, before taking inputs let us know what an array is first. Array: An array is a collection...

10 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.