Saturday, June 20, 2026
HomeLanguagesJavaClass hashCode() method in Java with Examples

Class hashCode() method in Java with Examples

The hashCode() method of java.lang.Class class is used to get the hashCode representation of this entity. This method returns an integer value which is the hashCode.

Syntax:

public int hashCode()

Parameter: This method does not accept any parameter.

Return Value: This method returns an integer value which is the hashCode.

Below programs demonstrate the hashCode() method.

Example 1:




// Java program to demonstrate hashCode() method
  
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = Class.forName("java.lang.String");
  
        System.out.println("Class represented by c1: "
                         + c1);
  
        // hashCode method on c1
        System.out.println("HashCode value: "
                           + c1.hashCode());
    }
}


Output:

Class represented by c1: class java.lang.String
HashCode value: 589431969

Example 2:




// Java program to demonstrate hashCode() method
  
public class Test {
    public static void main(String[] args)
        throws ClassNotFoundException
    {
        // returns the Class object for the class
        // with the specified name
        Class c1 = int.class;
  
        System.out.println("Class represented by c1: "
                         + c1);
  
        // hashCode method on c1
        System.out.println("HashCode value: "
                           + c1.hashCode());
    }
}


Output:

Class represented by c1: int
HashCode value: 589431969

Reference: https://docs.oracle.com/javase/9/docs/api/java/lang/Class.html#hashCode–

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

2 COMMENTS

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6899 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12110 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6965 POSTS0 COMMENTS