×

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

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Java copy file

There are for the most part 3 methods for duplicating documents utilizing java language. They are as given underneath: Utilizing File StreamUtilizing FileChannel ClassUtilizing Files class. 1. Using File Stream: Here we are...

5 minutes read.

Find next greater number with same set of digits in Java

It contains a number (num). Finding the smallest number that has the same number of elements as num that is also larger than num is the task at hand. If...

8 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Type Annotations in Java

Only declarations were eligible for annotations in earlier versions of Java. With Java 8, you may now annotate any type use, including types in declarations, generics, and casts: @Encrypted String data; List<@NonNull...

6 minutes read.

Java throws

Java throws: The Java throws keyword is used with the signature of the method to indicate that the method may raise an exception. The method that uses the Java throws...

3 minutes read.

Java float vs double

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...

7 minutes read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

How many ways to create object in Java?

In this article, you will be acknowledged about the different ways to create an object in java. So far you construct an object from a class, as is common knowledge,...

6 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

How to Convert String to boolean in Java

How to Convert String to boolean in Java There are two methods to convert String to boolean: Using parseBoolean(string) method Using valueOf(string) method If the string contains "True," "true," or "TRUE,"...

3 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Java Math multiplyFull() Method

The multiplyFull() method of Math class returns the exact product of the arguments. Syntax: public static long multiplyFull (int x, int y) Parameters: The parameters ‘x’ and ‘y’ represent the first value and second...

1 minute read.

How to Calculate the Time Difference between Two Dates in Java?

The date is used extensively in Java to calculate date discrepancies. The date of joining an organization, admittance, appointment, etc., can be included when creating the application. The differences between...

4 minutes read.

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long. Syntax: public static int decrementExact (int a) Parameters: The...

1 minute read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.