What is Objects.equals in Java?
The equals() method is a static method of the Objects class that accepts two objects and checks if the objects are equal.
- If both the objects point to
null, thenequals()returnstrue.
- If any one of the objects points to
null, thenequals()returnsfalse. - If both the objects are equal, then
equals()returnstrue. - Otherwise, we use the
equalsmethod of the first argument to determine equality.
To learn more about the
equalsmethod of theObjectsclass, refer here.
Syntax
public static boolean equals(Object a, Object b)
Parameters
-
Object a: first object. -
Object b: second object.
Return value
The method returns true if the objects are equal; otherwise, equals returns false.
Code
Example 1
In the code below, we pass null as an argument to the equals method. The method should return true, as both the input objects are null.
import java.util.Objects;public class main {public static void main(String[] args) {boolean equalityCheck = Objects.equals(null, null);System.out.println("The Objects.equals() method returns '" + equalityCheck + "' when the passed objects are null");}}
Example 2
In the code below, we pass null as one of the arguments for the method. The method should return false, as one of the input objects is null.
import java.util.Objects;public class main {public static void main(String[] args) {boolean equalityCheck = Objects.equals(null, 4);System.out.println("The Objects.equals() method returns '" + equalityCheck + "' when null is passed as the first object.");equalityCheck = Objects.equals(4, null);System.out.println("The Objects.equals() method returns '" + equalityCheck + "' when null is passed as the second object.");}}
Example 3
In the code below, we define a class Temp with attributes a and b. The methods toString(), hashCode, and equals are overridden in the class that has custom implementation.
Here, the equals method is overridden in the Temp class and checks if the objects have the same values for the attributes a and b.
- We create three different instances of the
Tempclass,temp1,temp2, andtemp3. - The instances
temp1andtemp2have the same values for the attributesaandb. Hence, passingtemp1andtemp2as parameters to the method should returntrue, as the values of the attributes of the objects are equal. - The instances
temp2andtemp3have different values for the attributesaandb. Hence, passingtemp2andtemp3as parameters to the method should returnfalse, as the values of the attributes of the objects are not equal.
import java.util.Objects;public class main {static class Temp{private int a, b;public Temp(int a, int b) {this.a = a;this.b = b;}@Overridepublic String toString() {return "Temp{" +"a=" + a +", b=" + b +'}';}@Overridepublic boolean equals(Object o) {if (this == o) return true;if (o == null || getClass() != o.getClass()) return false;Temp temp = (Temp) o;return a == temp.a && b == temp.b;}@Overridepublic int hashCode() {return Objects.hash(a, b);}}public static void main(String[] args) {Temp temp1 = new Temp(2,3);Temp temp2 = new Temp(2,3);Temp temp3 = new Temp(5,4);System.out.println(Objects.equals(temp1, temp2));System.out.println(Objects.equals(temp2, temp3));}}