Alex Rivera | Logout

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

Asked 2008-10-08T20:57:24.807
102

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

2 Answers

195

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)

answered 2012-02-20T15:31:00.907
23

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

answered 2008-10-08T21:10:40.350

Your Answer