Get Hash Code for Integer in Java


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());

Arushi
Arushi

e

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements