Field equals() method in Java with Examples
Last Updated :
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:
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws NoSuchFieldException,
SecurityException
{
Field[] fields
= User. class .getDeclaredFields();
Field fieldObj
= User. class .getField( "name" );
for ( int i = 0 ; i < fields.length; i++) {
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." );
}
}
}
}
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:
import java.lang.reflect.Field;
public class GFG {
public static void main(String[] args)
throws NoSuchFieldException,
SecurityException
{
Field[] fields1
= Class. class .getDeclaredFields();
Field[] fields2
= Class. class .getDeclaredFields();
for ( int i = 0 ; i < fields1.length; i++) {
for ( int j = 0 ; j < fields2.length; j++) {
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." );
}
}
}
}
}
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)
Share your thoughts in the comments
Please Login to comment...