Java - How to use Objects.equals()

Objects.equals() is a method that compares objects. It uses Object.equals() internally, and the patterns we have implemented so far when comparing objects are just made into a util class.

Lets see how to use Objects.equals()` and examples.

Source

equals() of the Objects class is implemented as follows.

package java.util;

public final class Objects {
  /**
   * Returns {@code true} if the arguments are equal to each other
   * and {@code false} otherwise.
   * Consequently, if both arguments are {@code null}, {@code true}
   * is returned and if exactly one argument is {@code null}, {@code
   * false} is returned.  Otherwise, equality is determined by using
   * the {@link Object#equals equals} method of the first
   * argument.
   *
   * @param a an object
   * @param b an object to be compared with {@code a} for equality
   * @return {@code true} if the arguments are equal to each other
   * and {@code false} otherwise
   * @see Object#equals(Object)
   */
  public static boolean equals(Object a, Object b) {
      return (a == b) || (a != null && a.equals(b));
  }
}

Even if you did not use the Objects class, it seems that everyone has experience using it as follows. Since the object calling equals() could be null, we had to throw a null exception.

String aa= "android";
String bb = "google";

if (aa.equals(bb)) {
    System.out.println("aa equal to bb");
}

if (aa != null && aa.equals(bb)) {
    System.out.println("aa equal to bb");
}

The Objects class is just a util class that was implemented directly as above. As you can see from the Objects.equals() code, comparing two null objects returns true.

example

Objects.equals() can be used like this:

String aa = null;
String bb = null;
if (Objects.equals(aa, bb)) {
  // ...
}
System.out.println("Objects.equals(null, null): " + Objects.equals(aa, bb));

aa = "google";
bb = "google";
System.out.println("Objects.equals(google, google): " + Objects.equals(aa, bb));

aa= "android";
bb = "google";
System.out.println("Objects.equals(android, google): " + Objects.equals(aa, bb));

result

Objects.equals(null, null): true
Objects.equals(google, google): true
Objects.equals(android, google): false

When comparing two null objects, Objects.equals returns true, so if you don`t like this you should implement something like this:

aa = null;
bb = "google";
if (aa != null && Objects.equals(aa, bb)) {
    System.out.println("non null and eqaul");
} else {
    System.out.println("null or not eqaul");
}

Alternatively, it can also be implemented using Objects.nonNull().

aa = null;
bb = "google";
if (Objects.nonNull(aa) && Objects.equals(aa, bb)) {
    System.out.println("non null and eqaul");
} else {
    System.out.println("null or not eqaul");
}

Note that Objects also supports nonNull() and isNull() methods.

public static boolean isNull(Object obj) {
    return obj == null;
}
public static boolean nonNull(Object obj) {
    return obj != null;
}

Reference

codechachaCopyright ©2019 codechacha