Field equals() method in Java with Examples

The equals() method of java.lang.reflect.Field is used to compare two field objects. This method compares two field objects and returns true if both objects are equal otherwise false. The two Field objects are considered equal if and only if when they were declared by the same class and have the same name and type. This method is very helpful at the time of debugging the object properties which are actually fields of a class in Java.

Syntax:

public boolean equals(Object obj)

Parameters: This method accepts one parameter obj which is the reference object to compare with this Feild object.



Return value: This method returns true if both objects are equal otherwise false.

Below programs illustrate equals() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get the array of Field objects
        Field[] fields
            = User.class.getDeclaredFields();
        Field fieldObj
            = User.class.getField("name");
  
        // print element of field array
        // and compare it with fieldObj
        for (int i = 0; i < fields.length; i++) {
  
            // compare the fields with each other
            boolean isEquals
                = fields[i].equals(fieldObj);
            if (isEquals) {
                System.out.println(
                    "Field -> ["
                    + fields[i] + "] and"
                    + " FieldObj -> ["
                    + fieldObj
                    + "] are equal.");
            }
            else {
                System.out.println(
                    "Field -> ["
                    + fields[i] + "] and"
                    + " FieldObj -> ["
                    + fieldObj
                    + "] are not equal.");
            }
        }
    }
}
  
// User class
class User {
  
    public String name;
    public int age;
}

chevron_right


Output:

Field -> [public java.lang.String User.name]
and
FieldObj -> [public java.lang.String User.name]
are equal.

Field -> [public int User.age]
and
FieldObj -> [public java.lang.String User.name]
are not equal.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate the above method
  
import java.lang.reflect.Field;
  
public class GFG {
  
    public static void main(String[] args)
        throws NoSuchFieldException,
               SecurityException
    {
  
        // get the array of Field objects
        Field[] fields1
            = Class.class.getDeclaredFields();
        Field[] fields2
            = Class.class.getDeclaredFields();
  
        // print element of field array 1 and compare
        // it with fields array 2
        for (int i = 0; i < fields1.length; i++) {
  
            for (int j = 0; j < fields2.length; j++) {
  
                // compare the fields with each other
                boolean isEquals
                    = fields1[i].equals(fields2[j]);
                if (isEquals) {
                    System.out.println(
                        "Field -> ["
                        + fields1[i] + "] and"
                        + " FieldObj -> ["
                        + fields2[j]
                        + "] are equal.");
                }
                else {
                    System.out.println(
                        "Field -> ["
                        + fields1[i] + "] and"
                        + " FieldObj -> ["
                        + fields2[j]
                        + "] are not equal.");
                }
            }
        }
    }
}
  
// Object of Class which contains
// noOfStudents and studentNames
class Class {
  
    public int noOfStudents;
    public String[] studentNames;
}

chevron_right


Output:

Field -> [public int Class.noOfStudents]
and
FieldObj -> [public int Class.noOfStudents]
are equal.

Field -> [public int Class.noOfStudents]
and
FieldObj -> [public java.lang.String[] Class.studentNames]
are not equal.

Field -> [public java.lang.String[] Class.studentNames]
and
FieldObj -> [public int Class.noOfStudents]
are not equal.

Field -> [public java.lang.String[] Class.studentNames]
and
FieldObj -> [public java.lang.String[] Class.studentNames]
are equal.

References: https://docs.oracle.com/javase/10/docs/api/java/lang/reflect/Field.html#equals(java.lang.Object)



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.