Comparison Two Booleans in Java?

Hello All, Can anyone suggest me, where I am wrong in this code? I have to compare two Boolean wrappers with each other. As a result I don’t know 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;
}

By using the java compiler on interviewbit, 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? Any Suggestions

Thanks in Advance!

Hi @Aarti_Yadav -

This is the forums for the Open edX project: https://openedx.org/

We please ask that forum posts stay on-topic about developing on or operating the Open edX software.

This seems to be a homework question in a language this project doesn’t use. I suggest you find a more appropriate forum for this question. Thank you!