Java BigInteger Class | hashCode() Method with Example

BigInteger Class hashCode() method: Here, we are going to learn about the hashCode() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 10, 2020

BigInteger Class hashCode() method

  • hashCode() method is available in java.math package.
  • hashCode() method is used to return the hash code value of this BigInteger.
  • hashCode() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • hashCode() method does not throw an exception at the time of returning hash code.

Syntax:

    public int hashCode();

Parameter(s):

  • None

Return value:

The return type of this method is int, it retrieves this BigInteger hash code value.

Example:

// Java program to demonstrate the example 
// of int hashCode() method of BigInteger

import java.math.*;

public class HashCodeOfBI {
    public static void main(String args[]) {
        // Initialize two variables str1 and str2
        String str1 = "101245";
        String str2 = "50145779658";

        // Initialize two BigInteger object 
        BigInteger b_int1 = new BigInteger(str1);
        BigInteger b_int2 = new BigInteger(str2);

        // Display b_int1 and b_int2
        System.out.println("b_int1: " + b_int1);
        System.out.println("b_int2: " + b_int2);

        System.out.println();

        // returns the hash code value of this
        // BigInteger b_int1
        int hash_code = b_int1.hashCode();
        System.out.println("b_int1.hashCode(): " + hash_code);

        // returns the hash code value of this
        // BigInteger b_int2
        hash_code = b_int2.hashCode();
        System.out.println("b_int2.hashCode(): " + hash_code);
    }
}

Output

b_int1: 101245
b_int2: 50145779658

b_int1.hashCode(): 101245
b_int2.hashCode(): -1393827553


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.