×

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

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Java Protected Keyword

An access modifier is a keyword that Java protects. It can be used to constructors, methods, inner classes, and variables. Variables, methods, and constructors that have been marked protected in...

3 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

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

Java String concat() method:

Java String concat() method is used to add the given String to the end of the current String. Syntax: public String concat(String str) Parameter: Str: String to be concatenated at the end of current...

1 minute read.

Difference between next() and nextline() in Java

One of the simplest methods for receiving input of the basic data types, also including int, double, and strings, in Java, is to use the Scanner class, which is part...

3 minutes read.

XOR of Array Elements Except Itself in Java

A lot of the biggest IT organisations, including The Google, Amazon, TCS, and The Accenture, etc., question about this issue in their interview processes. By addressing the issue, one hopes to assess...

5 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Iccanobif Numbers in Java

In this article, you will be very well equipped with the knowledge of iccanobif numbers in Java. You will also know how they are formed and basic example programs on...

3 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

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

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other...

2 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Bean class in Java

The web applications that are created with the help of the JSP or Java Server Pages generally have fairly more functionality than the web pages that specifically are created with...

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

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

Sorting Algorithms in Java

Sorting Algorithms in Java Sorting is the technique that puts the elements of an array or list either in descending or ascending order. For example, take an array A, whose elements...

3 minutes read.