Java Float Hashcode Exploration

JavaJavaBeginner
Practice Now

Introduction

In this lab, you will learn about the hashCode() method in Java's Float class. This method is used to get the hash code of a float value. A hash code is a unique integer value that is associated with every object in Java. This lab will guide you through different code examples on how to use the hashCode() method.

Create a new Java file

First, create a new Java file at ~/project/FloatHashcode.java using the following command:

touch ~/project/FloatHashcode.java

Define Float variables

In this step, define two Float variables and initialize them with two different float values. Also, import the Float class from java.lang package.

import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        Float n1 = 2.3456F;
        Float n2 = -4.567F;
    }
}

Get the hash code using Float's hashCode() method

In this step, use the hashCode() method of the Float class to get the hash code of the Float variables defined in the previous step.

import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        Float n1 = 2.3456F;
        Float n2 = -4.567F;

        int hash1 = n1.hashCode(); // get hash code of n1
        int hash2 = n2.hashCode(); // get hash code of n2

        System.out.println("Hash code for n1 is " + hash1);
        System.out.println("Hash code for n2 is " + hash2);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Get hash code for user input

In this step, create a program that prompts the user to enter a float value, gets the hash code of the input using the Float class's hashCode() method, and then prints the hash code to the console.

import java.util.Scanner;
import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a float value: ");
        float input = scanner.nextFloat();
        Float n = input;

        int hashCode = n.hashCode();

        System.out.println("Hash code for the input " + input + " is " + hashCode);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Compare hash codes of two Float values

In this step, create a program that compares the hash codes of two Float values.

import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        Float n1 = 2.3456F;
        Float n2 = 2.3456F;

        int hash1 = n1.hashCode();
        int hash2 = n2.hashCode();

        if (hash1 == hash2) {
            System.out.println("The hash codes of n1 and n2 are equal");
        } else {
            System.out.println("The hash codes of n1 and n2 are not equal");
        }
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Override the hashCode() method in a class

In this step, we will override the hashCode() method in a class. Here, we create a MyClass class with two float variables a and b. We then override the hashCode() method in the class and calculate the hash code of the object using the formula (31 * Float.floatToIntBits(a)) + Float.floatToIntBits(b).

import java.lang.Float;

class MyClass {
    float a;
    float b;

    public MyClass(float a, float b) {
        this.a = a;
        this.b = b;
    }

    @Override
    public int hashCode() {
        return (31 * Float.floatToIntBits(a)) + Float.floatToIntBits(b);
    }
}

public class FloatHashcode {
    public static void main(String[] args) {
        MyClass obj = new MyClass(2.3456F, -4.567F);

        int hash = obj.hashCode();

        System.out.println("Hash code for obj is " + hash);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Handle null pointers

In this step, create a program that handles null pointers while getting the hash code of a Float object.

import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        Float n = null;

        int hash = (n != null) ? n.hashCode() : 0;

        System.out.println("Hash code for n is " + hash);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Use Objects.hashCode() to get hash code

In this step, we use the Objects.hashCode() method to get the hash code of a Float object. This method returns a hash code of any object passed to it and handles null pointers gracefully.

import java.lang.Float;
import java.util.Objects;

public class FloatHashcode {
    public static void main(String[] args) {
        Float n = null;

        int hash = Objects.hashCode(n);

        System.out.println("Hash code for n is " + hash);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Use Float.hashCode(float) for primitive floats

In this step, we use the Float.hashCode(float) method to get the hash code of a primitive float value. This method returns the same hash code that is returned by the floatToIntBits(float) method.

import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        float f = 2.3456F;

        int hash = Float.hashCode(f);

        System.out.println("Hash code for f is " + hash);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Use Float.floatToIntBits(float) to get integer representation of float

In this step, we use the Float.floatToIntBits(float) method to get the integer representation of a float value. This method returns the same integer representation that is used to calculate the hash code of a float value.

import java.lang.Float;

public class FloatHashcode {
    public static void main(String[] args) {
        float f = 2.3456F;

        int intBits = Float.floatToIntBits(f);

        System.out.println("Integer representation of f is " + intBits);
    }
}

To compile and run the file, execute the following commands in the terminal:

javac ~/project/FloatHashcode.java
java FloatHashcode

Summary

In this lab, you learned about the hashCode() method of the Float class in Java. You learned how to get the hash code of a float value, override the hashCode() method in a class, use Objects.hashCode() method, and handle null pointers while getting the hash code of an object. You also learned about the floatToIntBits(float) method, which is used to get the integer representation of a float value.

Other Java Tutorials you may like