Signup/Sign In
LAST UPDATED: AUGUST 26, 2020

Java Double hashCode(double value) method

The hashCode(double value) method is compatible with Java 1.8 or more version and is a part of the Double class of the java.lang package. It is used to return the hash code of the double passed as the argument.

This method is compatible with Double.hashCode() of Double class.

Syntax:

public static int hashCode(double value)  

Parameter:

The parameter includes the double value of which the hash code is to be generated.

Returns:

The unique integer value (hash code) associated with the double value passed as an argument.

Example 1:

Here, using the hashCode(double value) method, the double value passed is converted into its respective hashcode.

import java.lang.Double;

public class StudyTonight

{  
    public static void main(String[] args)  
    {  
        int hv1 = Double.hashCode(27.56930); //generate the hashcode of the passed argument
        int hv2 = Double.hashCode(-81.4889); 
        System.out.println("Hash code Value is: " + hv1); 
        System.out.println("Hash code Value is: " + hv2);
    }  
}


Hash code Value is: -450228899
Hash code Value is: -479354970

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 the value: ");  
         Scanner sc = new Scanner(System.in);         
         double i = sc.nextDouble();  
         int hv = Double.hashCode(i);  // Returning hash code value for this object
         System.out.println("Hash code is: " + hv);
           
      }
      catch(Exception e)
      {
        System.out.println("Invalid Input!!");
      }
        
    }  
}  


Enter the value: 89.55
Hash code is: 1936019456
*******************************
Enter the value: -664.8
Hash code is: -1495097344
*******************************
Enter the value: 0x667
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.