Constructor hashCode() method in Java with Examples

The hashCode() method of java.lang.reflect.Constructor class is used to return a hashcode for this Constructor object. The hashcode is always the same if the constructed object doesn’t change. Hashcode is a unique code generated by the JVM at the time of class object creation. We can use hashcode to perform some operation on hashing related algorithms like a hashtable, hashmap, etc. We can search for an object with that unique code.

Syntax:

public int hashCode()

Parameters: This method accepts nothing.

Return: This method returns a hash code integer value for this object.

Below programs illustrate hashCode() method:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate hashCode() method
  
import java.lang.reflect.Constructor;
import java.util.ArrayList;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = ArrayList.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        // get hash code of this constructor class
        int code = cons[0].hashCode();
  
        // print result
        System.out.println(
            "Hash Code count = " + code);
    }
}

chevron_right


Output:

Hash Code count = -1114099497

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to illustrate hashCode() method
  
import java.lang.reflect.Constructor;
  
public class GFG {
  
    public static void main(String[] args)
    {
        // create a class object
        Class classObj = String.class;
  
        // get Constructor object
        // array from class object
        Constructor[] cons = classObj.getConstructors();
  
        // get hash code of this constructor class
        int code = cons[0].hashCode();
  
        // print result
        System.out.println(
            "Hash Code count for string class"
            + " constructor = " + code);
    }
}

chevron_right


Output:

Hash Code count for string class constructor = 1195259493

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Constructor.html#hashCode()

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.