Is there a Java standard "both null or equal" static method?

Chris Conway picture Chris Conway · Oct 8, 2008 · Viewed 33.5k times · Source

To save some typing and clarify my code, is there a standard version of the following method?

public static boolean bothNullOrEqual(Object x, Object y) {
  return ( x == null ? y == null : x.equals(y) );
}

Answer

Kdeveloper picture Kdeveloper · Feb 20, 2012

With Java 7 you can now directly do a null safe equals:

Objects.equals(x, y)

(The Jakarta Commons library ObjectUtils.equals() has become obsolete with Java 7)