Saturday, September 6, 2025
HomeLanguagesJavaInteger hashCode() Method in Java

Integer hashCode() Method in Java

The java.lang.Integer.hashCode() method of Integer class in Java is used to return the hash code for a particular Integer .

Syntax:

public int hashCode()

Parameters : The method does not take any parameters.

Return Value: The method returns a hash code integer value for this object, which is equal to the uncomplicated primitive integer value, represented by this Integer object.

Below programs illustrate the use of hashCode() of Integer class:
Program 1: When integer data type is passed.




// Java program to demonstrate working
// of Java.lang.Integer.hashCode() Method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
        // Object s_int created
        Integer s_int = new Integer("223");
  
        // Returning a hash code value for this object 
        int hashcodevalue = s_int.hashCode();
        System.out.println("Hash code Value for object = " + hashcodevalue);
    }
}


Output:

Hash code Value for object = 223

Program 2: When String data type is passed.
Note: This causes RuntimeErrors like NumberFormatException




// Java program to demonstrate working
// of Java.lang.Integer.hashCode() Method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
        // object s_int created
        Integer s_int = new Integer("gfg");
  
        // Returning a hash code value for this object.
        int hashcodevalue = s_int.hashCode();
        System.out.println("Hash code Value for object = " + hashcodevalue);
    }
}


Output:

Exception in thread "main" java.lang.NumberFormatException: For input string: "gfg"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.(Integer.java:867)
    at Geeks.main(Geeks.java:9)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32270 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6639 POSTS0 COMMENTS
Nicole Veronica
11803 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS