Signup/Sign In

Java Double hashCode() method

The hashCode() method is a part of the Double class of the java.lang package. It is used to return the hash code of a given input. A hashcode is defined as a unique primitive integer value that is associated with every object in Java.

The result returned by this method is the exclusive OR of the two halves of the long integer bit representation and returns the same value as returned by the doubleToLongBits() method. This method overrides the method of Object class.

Syntax:

public int hashCode() 

Parameter:

No parameter is passed in this method.

Returns:

The equivalent primitive integer value (hash code) associated with the Double-object.

Example 1:

Here, using the hashCode() method, the Double objects are converted into its respective hashcode.

import java.lang.Double;

public class StudyTonight
{  
    public static void main(String[] args)  
    {  
        
        Double n1 = 81.69445;
        Double n2 = -27.90355;
           
        int hv1 = n1.hashCode();//Returning the hash code value for the object n1
        int hv2 = n2.hashCode();//Returning the hash code value for the object n2
        System.out.println("Hash code Value : " + hv1);
        System.out.println("Hash code Value : " + hv2);  
    }  
}  


Hash code Value : -1640120014
Hash code Value : -843077042

Example 2:

Here is a user-defined example where anyone using this code can put a value of his choice and get the equivalent hash code.

import java.util.Scanner;  

public class StudyTonight
{  
    public static void main(String[] args)
      {  
        try
        {
          System.out.print("Enter input: ");  
          Scanner sc = new Scanner(System.in);         
          Double n = sc.nextDouble();  
          int hv = n.hashCode(); //Returning the hash code value for the object 
          System.out.println("Hash code Value for the given input is: " + hv);
          
        }
        catch(Exception e)
        {
          System.out.println("Invalid Input!!");
        }
      
      }  
}  


Enter input: 89.55
Hash code Value for the given input is: 1936019456
**********************************************************
Enter input: -77.09
Hash code Value for the given input is: 1326411060
*********************************************************
Enter input: 0x556
Invalid Input!!

Live Example:

Here, you can test the live code example. You can execute the example for different values, even can edit and write your examples to test the Java code.



About the author:
A Computer Science and Engineering Graduate(2016-2020) from JSSATE Noida. JAVA is Love. Sincerely Followed Sachin Tendulkar as a child, M S Dhoni as a teenager, and Virat Kohli as an adult.