Java Integer hashCode() Method

Java Integer hashCode() Method | Here, we will discuss the hashCode() method given in the Integer class. This method converts the integer to the particular hash code value. The hashCode() method is defined in the java.lang.Object class, and since every Java class is a sub-class of the Object class therefore it is also available to the Integer class. The integer class overrides this method.

There are two variations of the hashCode() method in the Java Integer class. They are as follows:-

  • public int hashCode()
  • public int hashCode(int val)

Parameters:-
val: this is an integer variable passed directly to the method.
Return type: integer.
Returns: the hash value of the passed integer.

A hash code value for this Integer object is equal to the primitive int value represented by this Integer object.

Java Integer hashCode() Method Example

public class Main {
    public static void main(String[] args) {
        Integer num = Integer.valueOf(923);
        int val = num.hashCode();
        System.out.println("Hash code value for the given Integer: " + val);
    }
}

Output:-

Hash code value for the given Integer: 923

public class Main {
    public static void main(String[] args) {
        Integer num = -500;
        System.out.println("Hash code value: " + num.hashCode());
    }
}

Output:-

Hash code value: -500

public class Main {
    public static void main(String[] args) {
        Integer num1 = Integer.MIN_VALUE;
        Integer num2 = Integer.MAX_VALUE;
        System.out.println("Hash code value: " + num1.hashCode());
        System.out.println("Hash code value: " + num2.hashCode());
    }
}

Output:-

Hash code value: -2147483648
Hash code value: 2147483647

Hashcode For Integer In Java using System.identityHashCode()

The Integer class hashCode() method always returns the int value of the given object. But if we want to get the memory address then we can use identityHashCode() method defined in the System class. The System.identityHashCode() method is used to get the reference-based hashcode value of the passed object. The method is declared as follows:- public static int identityHashCode(Object num)

public class Main {
    public static void main(String[] args) {
        Integer num1 = Integer.valueOf(100);
        Integer num2 = -300;
        System.out.println(System.identityHashCode(num1));
        System.out.println(System.identityHashCode(num2));
    }
}

Output:

1940447180
476402209

You may get different results because it returns the memory address which varies from computer to computer.

public class Main {
    public static void main(String[] args) {
        Integer num1 = Integer.MIN_VALUE;
        Integer num2 = Integer.MAX_VALUE;
        System.out.println(System.identityHashCode(num1));
        System.out.println(System.identityHashCode(num2));
    }
}

Output:-

1940447180
476402209

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *