Field equals() method in Java with Examples - GeeksforGeeks

05 Nov, 2019
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 classclass User {      public String name;    public int age;}
Field -> [public java.lang.String User.name]andFieldObj -> [public java.lang.String User.name]are equal.
Field -> [public int User.age]andFieldObj -> [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 studentNamesclass Class {      public int noOfStudents;    public String[] studentNames;}
Field -> [public int Class.noOfStudents]andFieldObj -> [public int Class.noOfStudents]are equal.
Field -> [public int Class.noOfStudents]andFieldObj -> [public java.lang.String[] Class.studentNames]are not equal.
Field -> [public java.lang.String[] Class.studentNames]andFieldObj -> [public int Class.noOfStudents]are not equal.
Field -> [public java.lang.String[] Class.studentNames]andFieldObj -> [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)
shubham_singh
Java-Field
Java-Functions
java-lang-reflect-package
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java