Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaField hashCode() method in Java with Examples

Field hashCode() method in Java with Examples

The hashCode() method of java.lang.reflect.Field used to get the hashcode for this Field. Final Hashcode is computed as the exclusive-or of the hashcodes for the underlying field’s declaring class name and its name. The hashcode is always the same if the object doesn’t change. Hashcode is a unique code generated by the JVM at the time of object creation. It can be used to perform some operation on hashing related algorithm like a hashtable, hashmap etc. An object can also be searched with this unique code.

Syntax:

public int hashCode()

Parameters: This method accepts nothing.

Return value: This method returns an integer which is the hash code value for this object.

Below programs illustrate hashCode() method:
Program 1:




// Java program to demonstrate hashCode() method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Get the marks field object
        Field field = User.class.getField("Marks");
  
        // Apply hashCode Method on User Object
        // to get the hashCode of Marks field
        int value = field.hashCode();
  
        // print result
        System.out.println("HashCode of Marks field"
                           + " is " + value);
  
        // Now Get the Fees field object
        field = User.class.getField("Fees");
  
        // Apply hashCode Method on User Object
        // to get the hashcode of Fees field
        value = field.hashCode();
  
        // print result
        System.out.println("HashCode of Fees field"
                           + " is " + value);
  
        // Now Get the name field object
        field = User.class.getField("name");
  
        // Apply hashCode Method on User Object
        // to get the hashCode of name field
        value = field.hashCode();
  
        // print result
        System.out.println("HashCode of name field"
                           + " is " + value);
    }
}
  
// sample User class
class User {
  
    // static double values
    public static double Marks = 34.13;
    public static float Fees = 3413.99f;
    public static String name = "Aman";
  
    public static double getMarks()
    {
        return Marks;
    }
  
    public static void setMarks(double marks)
    {
        Marks = marks;
    }
  
    public static float getFees()
    {
        return Fees;
    }
  
    public static void setFees(float fees)
    {
        Fees = fees;
    }
  
    public static String getName()
    {
        return name;
    }
  
    public static void setName(String name)
    {
        User.name = name;
    }
}


Output:

HashCode of Marks field is 71482573
HashCode of Fees field is 591398
HashCode of name field is 1779040

Program 2:




// Java program to demonstrate hashCode() method
  
import java.lang.reflect.Field;
import java.time.Month;
  
public class GFG {
  
    public static void main(String[] args)
        throws Exception
    {
  
        // Get all field objects of Month class
        Field[] fields = Month.class.getFields();
  
        for (int i = 0; i < fields.length; i++) {
  
            // print name of Fields
            System.out.println("HashCode of Field: "
                               + fields[i].hashCode());
        }
    }
}


Output:

HashCode of Field: -297508095
HashCode of Field: 1296412905
HashCode of Field: 1475695976
HashCode of Field: 1343692077
HashCode of Field: 1404020238
HashCode of Field: 1401709321
HashCode of Field: 1401709395
HashCode of Field: 538235208
HashCode of Field: 2125827066
HashCode of Field: -1718938229
HashCode of Field: -1007182215
HashCode of Field: 59532142

References: https://docs.oracle.com/javase/8/docs/api/java/lang/reflect/Field.html#hashCode–

RELATED ARTICLES

Most Popular

Recent Comments