Signup/Sign In
LAST UPDATED: AUGUST 29, 2020

Java Float hashCode() method

The hashCode() method is a part of the Float 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 same integer representation which is returned by the floatToIntBits(float) 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 Float object.

Example 1:

Here, using the hashCode() function, hashcode value of Float value is returned.

import java.lang.Float;

public class StudyTonight
{  
    public static void main(String[] args)  
    {   
        Float n1 = 81.69445f;
        Float n2 = -27.90355f;
    
        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 : 1118004111
Hash code Value : -1042335112

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);         
          Float n = sc.nextFloat();  
          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: 45.89034
Hash code Value for the given input is: 1110937525
*******************************************************
Enter input: -90.34
Hash code Value for the given input is: -1028346348
*******************************************************
Enter input: 0x98
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.