×

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

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Packages in Java

Packages in Java can be defined as an assortment for grouping various classes and interfaces based on their performance. It is a catalog for holding various java files. They provide...

4 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Java Math cbrt() Method

The cbrt() method of Math class returns the cube root of a double value. Syntax: public static double cbrt(double a) Parameters: The parameter ‘a’ represents the value whose cube root is to be determined. Return...

2 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Java exception list

Java uses exceptions, like the majority of contemporary programming languages, to deal with both errors and "extraordinary events." When an exception arises inside the program, it messes up the regular...

6 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

How to handle NullPointerException in Java

Introduction: Throwable class is the super or root class of the java exception hierarchy which contains two sub-classes. The classes’ are- ExceptionError Exception: If you are doing any wrong things in a...

3 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

3 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Palindrome number program in Java

Write java program to check if a number is palindrome in Java? A number that does not change on reversing is called a palindrome number. In other words, when reversing the...

3 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.

Java Integer compare() method

The compare() method of Integer class compares the two specified int values. Syntax public static int compare(int x, int y) Parameters The parameters ‘x’ and ‘y’ represent the first and second int values to...

2 minutes read.

Java Math nextUp() Method

The nextUp() method of Math class returns the floating-point number adjacent to the argument in direction of the positive infinity. Syntax: public static double nextUp (double d)public static float nextUp (float f) Parameters: The...

2 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.