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. 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.. 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. 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. 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.