0

I have to compare two Boolean wrappers with each other. As a result I want to know if they are equal or not.

This is what I came up with:

public static boolean areEqual(final Boolean a, final Boolean b) {
    if (a == b) {
        return true;
    }

    if (a != null && b != null) {
        return a.booleanValue() == b.booleanValue();
    }

    return false;
}

Is there a better and/or shorter way to correctly compare two Boolean wrappers for equality?

First I wanted to use Object.equals() or Boolean.compareTo() but both ways could end up in a NullPointerException, right? Maybe there is something that I don't see here, that's why I'm asking.

winklerrr
  • 13,026
  • 8
  • 71
  • 88
  • You're better off writing code such that your booleans _can't_ be null (or if they are, your code fails and NPEs out, since that would be a bug). Then you can compare them with equals like a sane person. – pvg Sep 12 '17 at 18:37
  • Check this [question](https://stackoverflow.com/questions/31366231/how-to-check-if-two-boolean-values-are-equal) – Laf Sep 12 '17 at 18:39
  • @pvg I have to use Boolean wrappers, that's the problem. – winklerrr Sep 12 '17 at 18:41
  • 1
    @Laf your linked answer just takes care of two primitive booleans. – winklerrr Sep 12 '17 at 18:49
  • @winklerrr that's fine. What I'm suggesting is you enforce non-nullness once, someplace. The `java.util.Objects` approach works fine but you want to use that sparingly because you're essentially peppering your code with implicit null checks making it harder to determine the root cause of possibly invalid state. If you enforce (at the relevant abstraction barrier/interface) non-nullness, you don't have to worry about it again. An NPE just tells you you have a bug, rather than hiding it with null checks. – pvg Sep 12 '17 at 18:49
  • Could the downvoters please explain why? – winklerrr Sep 13 '17 at 11:48

3 Answers3

13

The shortest you can get, with null safety (works naturally for any other objects that implement equals()):

java.util.Objects.equals(a, b);
Kayaman
  • 72,141
  • 5
  • 83
  • 121
1

There is no need for you to reinvent the wheel.

If you are using Java 7 or later, use the java.util.Objects class (as mentioned by Kayaman).

If you are using an earlier version of java, use the Apache BooleanUtils class. Try a google search for "apache booleanutils" to find out how to get it.

Edit: corrected java version.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • If I would use an earlier version than Java 7, I would mark your answer as the correct one, because it covers more scenarios. – winklerrr Sep 12 '17 at 18:52
-1

It is working for me.

 public class MyClass {
  public static void main(String args[]) {
    Boolean x=true;
    Boolean y=true;
    System.out.println(compare(x,y)); // true
    x=false;
    y=true;
    System.out.println(compare(x,y)); // false
    x=true;
    y=false;
    System.out.println(compare(x,y)); // false
    x=false;
    y=false;
    System.out.println(compare(x,y)); // true
    x=null;
    y=null;
    System.out.println(compare(x,y)); // true
  }
  public static boolean compare(final Boolean a, final Boolean b) {
    if (a == null || b == null) {
      return a == b;
    }
    return a.equals(b);
  }
}

Probe with https://www.jdoodle.com/online-java-compiler

Edit: add exception to null values.

juusechec
  • 69
  • 8
  • 1
    It's working because you use the primitive booleans and not their wrappers. You have to pay attention on `b` vs `B`: `boolean` vs `Boolean` – winklerrr Sep 12 '17 at 18:45
  • Casting can be easily done with `Boolean.booleanValue()`, I know, but this still throws a `NullPointerException` and therefore needs more than the actual cast. – winklerrr Sep 12 '17 at 18:47
  • Yes in this https://stackoverflow.com/questions/3728616/boolean-vs-boolean-in-java – juusechec Sep 12 '17 at 18:47
  • Did you even read what you linked? Your linked answer has nothing to do with my question. – winklerrr Sep 12 '17 at 18:55
  • Has the explain of difference between to types. – juusechec Sep 12 '17 at 18:58
  • Yeah, but I didn't ask about the difference between `boolean` and `Boolean`. Please read the question again. – winklerrr Sep 12 '17 at 18:59