Java.lang Package Classes

Java Integer - hashCode() Method



The java.lang.Integer.hashCode() method returns a hash code for this Integer.

Syntax

public int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code value for this object, equal to the primitive int value represented by this Integer object.

Exception

NA.

Example:

In the example below, the java.lang.Integer.hashCode() method returns a hash code for the given Integer.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating Integer objects
    Integer val1 = 25;
    Integer val2 = -25;

    //printing hashcode of the Integer objects
    System.out.println("hashCode of the val1 is: " + val1.hashCode()); 
    System.out.println("hashCode of the val2 is: " + val2.hashCode());   
  }
}

The output of the above code will be:

hashCode of the val1 is: 25
hashCode of the val2 is: -25

❮ Java.lang - Integer