Is there any ready implementation of method
public static boolean equals(Object o1, Object o2) {
return o1==null && o2==null || o1!=null && o2!=null && o1.equals(o2);
}
somewhere in JRE/JDK?
If you are on JDK 7..
Objects.equals(a,b)
That's Objects
with an s
, which is in java.util
, as in the utility library for objects, similar to Collections
with an s
and Arrays
with an s
.
o1 == null ? o2 == null : o1.equals(o2)
but not everybody likes using '?'. On the other hand, with JDK 7 Objects is the better way as @niels-bech-nielsen pointed in his answer.return (o1 == o2) || (o1 != null && o1.equals(o2));
:) which is what is implemented in JDK as well.