Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if two boolean values are equal?

I need a method which I can call within the junit assertTrue() method which compares two booleans to check if they are equal, returning a boolean value. For example, something like this:

boolean isEqual = Boolean.equals(bool1, bool2);

which should return false if they are not equal, or true if they are. I've checked out the Boolean class but the only one that comes close is Boolean.compare() which returns an int value, which I can't use.

like image 930
user3541263 Avatar asked Jul 12 '15 09:07

user3541263


People also ask

How do you know if two booleans are equal?

boolean isEqual = Boolean. equals(bool1, bool2); which should return false if they are not equal, or true if they are.

How do you compare two boolean values?

We use the compare() method of the BooleanUtils class to compare two boolean values. The method takes two values and returns true if both the values are the same. Otherwise, it returns false .

Can you use == for Booleans?

Boolean values are values that evaluate to either true or false , and are represented by the boolean data type. Boolean expressions are very similar to mathematical expressions, but instead of using mathematical operators such as "+" or "-", you use comparative or boolean operators such as "==" or "!".

Does == return a boolean?

The equals() method of Java Boolean class returns a Boolean value. It returns true if the argument is not null and is a Boolean object that represents the same Boolean value as this object, else it returns false.


1 Answers

The == operator works with booleans.

boolean isEqual = (bool1 == bool2);

(The parentheses are unnecessary, but help make it easier to read.)

like image 175
user253751 Avatar answered Oct 23 '22 01:10

user253751