This blog will discuss the usage of the “Objects.equals()” method in Java.
What is “Objects.equals()” in Java?
The associated “equals()” method is a static method of the “Objects class” that takes two objects as its parameters and checks if they are equal by returning a “boolean” value.
Syntax
In the above syntax, “obj” refers to the object that needs to be compared with the associated object.
Example 1: Comparing the Set “Null”, “Integer”, and “Float” Values Using “Objects.equals()” in Java
In this example, the values of different data types will be allocated as methods, i.e., “Objects.equals()” parameter, and checked for equality:
boolean returnComp1 = Objects.equals(null, 4);
boolean returnComp2 = Objects.equals(3.5, 4);
boolean returnComp3 = Objects.equals(4, 4);
System.out.println("The first comparison becomes: " + returnComp1);
System.out.println("The second comparison becomes: " + returnComp2);
System.out.println("The second comparison becomes: " + returnComp3);
In the above lines of code, apply the following steps:
- Firstly, associate the “boolean” keyword with the allocated values to store the outcome in the form of boolean values “true” or “false”.
- Also, apply the “Objects.equals()” method and set the “null”, “integer”, and “float” values, respectively, to be checked for equality.
- Lastly, display the resultant output based on the performed comparison in each case.
Output
In the above output, it can be seen that the corresponding outcome is returned based on the evaluated comparison.
Note: If the values are the same, but the data types are conflicting, i.e., 4(integer), 4.0(float), the outcome will be returned as “false”.
Example 2: Using the “Objects.equals()” Method With String Objects in Java
In this particular example, the discussed method can be utilized to compare the created string objects without and with allocating the two string values, respectively:
In the above code snippet:
- First of all, create the two “String” objects named “object1” and “object2”, respectively.
- In the next step, associate the “equals()” method with the former object and place the latter object as its(method) parameter to check if the created objects are equal before setting the values.
- After that, allocate the stated string values to both the created objects.
- Lastly, compare both objects again after the values are assigned to observe the change in comparison.
Output
In this output, it can be analyzed that since the objects were not allocated with the values first, so the boolean value “true” is returned, which is not the case in the latter scenario(after the values are set).
Conclusion
The “Objects.equals()” in Java is a static method that takes two objects as its parameters and checks if they are equal by returning a boolean value. This method can be applied to compare the values of multiple data types or to compare the two objects with and without allocating the values to them(objects). This blog guided the usage and implementation of the “Objects.equals()” method in Java.