Open In App

Field equals() method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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 Field object.

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

Below programs illustrate equals() method:
Program 1:




// 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;
}


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:




// 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;
}


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)



Last Updated : 05 Nov, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads