Introduction
In this lab, we are going to learn about the hashCode(float n) method of the Float class in Java. This method is used to generate the hash code of the float passed as the argument. We will cover how to use this method to generate hash codes of different float values with examples.
Create a new Java file
Firstly, we need to create a new Java file in the ~/project/ directory. Open the terminal and type the following command:
cd ~/project/
touch FloatHashCode.java
Write Java code to generate hash code of a predefined float value
In this step, we will write Java code to calculate the hash code of a predefined float value using the hashCode(float n) method. We will use System.out.println() to print the hash code.
public class FloatHashCode {
public static void main(String[] args) {
float floatValue = 25.6789f;
int hashCode = Float.hashCode(floatValue);
System.out.println("Hash code of " + floatValue + " is: " + hashCode);
}
}
Compile and Run the Java code
Now, we will compile the Java code using the following command:
javac FloatHashCode.java
Run the code with the following command:
java FloatHashCode
Output:
Hash code of 25.6789 is: 1092361858
Write Java code to generate hash code using user input
In this step, we will write Java code to accept user input and generate the hash code of the entered float value using the hashCode(float n) method. We will use the Scanner class to read user input.
import java.util.Scanner;
public class FloatHashCode {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a float value: ");
float floatValue = scanner.nextFloat();
int hashCode = Float.hashCode(floatValue);
scanner.close();
System.out.println("Hash code of " + floatValue + " is: " + hashCode);
} catch (Exception e) {
System.out.println("Invalid input: " + e);
}
}
}
Compile and Run the Java code
Now, we will compile the modified Java code using the following command:
javac FloatHashCode.java
Run the code with the following command:
java FloatHashCode
Output:
Enter a float value: 12.35
Hash code of 12.35 is: 1091324561
Write Java code to generate hash code of negative float value
In this step, we will write Java code to calculate the hash code of a negative float value using the hashCode(float n) method.
public class FloatHashCode {
public static void main(String[] args) {
float floatValue = -9.876f;
int hashCode = Float.hashCode(floatValue);
System.out.println("Hash code of " + floatValue + " is: " + hashCode);
}
}
Compile and run the Java code
Now, we will compile and run the modified Java code using the following command:
javac FloatHashCode.java
java FloatHashCode
Output:
Hash code of -9.876 is: -1929053930
Write Java code to generate hash code of 0 float value
In this step, we will write Java code to calculate the hash code of 0 float value using the hashCode(float n) method.
public class FloatHashCode {
public static void main(String[] args) {
float floatValue = 0.0f;
int hashCode = Float.hashCode(floatValue);
System.out.println("Hash code of " + floatValue + " is: " + hashCode);
}
}
Compile and run the Java code
Now, we will compile and run the modified Java code using the following command:
javac FloatHashCode.java
java FloatHashCode
Output:
Hash code of 0.0 is: 0
Summary
In this lab, we learned about the hashCode(float n) method of the Float class in Java. We learned how to generate hash codes of different float values, including negative floats and zero floats, using this method. We also learned how to accept user input to calculate hash code.



