Get Hash Code for Integer in Java

Java 8Object Oriented ProgrammingProgramming

The HashCode for an Integer can be obtained using the hashCode() method in Java. This method does not accept any parameters and it returns a hash code value of the Integer object.

A program that demonstrates the hashCode() method in Java is given as follows:

Example

 Live Demo

import java.lang.*;
public class Demo {
   public static void main(String args[]) {
      Integer i = new Integer(60);
      System.out.println("The integer value is: " + i);
      System.out.println("The Hashcode for the above value is: " + i.hashCode());
   }
}

Output

The integer value is: 60
The Hashcode for the above value is: 60

Now let us understand the above program.

The Integer i is defined. Then the Integer value is printed and its HashCode is also printed using the hashCode() method. A code snippet which demonstrates this is as follows:

Integer i = new Integer(60);
System.out.println("The integer value is: " + i);
System.out.println("The Hashcode for the above value is: " + i.hashCode());
raja
Published on 19-Feb-2019 05:48:04
Advertisements