2

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?

5
  • 1
    You've just implemented it.
    – Maroun
    Commented Mar 13, 2014 at 9:13
  • 1
    What would it mean if there was? You want a standalone equals you'd have to call as a utility? Commented Mar 13, 2014 at 9:14
  • Please note that JDK includes the JRE..
    – Maroun
    Commented Mar 13, 2014 at 9:15
  • You could do it a little more compact with 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.
    – joragupra
    Commented Mar 13, 2014 at 9:17
  • The logic can be shortened to return (o1 == o2) || (o1 != null && o1.equals(o2)); :) which is what is implemented in JDK as well.
    – Rahul
    Commented Mar 13, 2014 at 9:20

1 Answer 1

9

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.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.