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

JavaNullEquals

Java Problem Overview


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) );
}

Java Solutions


Solution 1 - Java

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)

Solution 2 - Java

if by some chance you are have access to the Jakarta Commons library there is ObjectUtils.equals() and lots of other useful functions.

EDIT: misread the question initially

Solution 3 - Java

If you are using <1.7 but have Guava available: Objects.equal(x, y)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChris ConwayView Question on Stackoverflow
Solution 1 - JavaKdeveloperView Answer on Stackoverflow
Solution 2 - JavaMattView Answer on Stackoverflow
Solution 3 - JavaSam BerryView Answer on Stackoverflow