Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare Boolean?

Tags:

java

boolean

Take this for example (excerpt from Java regex checker not working):

while(!checker) {     matcher = pattern.matcher(number);     if(matcher.find())         checker = true;     else         year++; } 

Would it matter if .equals(false) was used to check for the value of the Boolean checker?

I know that there is this which is rather similar. However, obviously the question deals with primitive boolean and not the object wrapper, Boolean; thus, .equals() would not be applicable.

Also, should Boolean be dealt differently than boolean?

like image 701
ylun.ca Avatar asked Mar 18 '14 21:03

ylun.ca


People also ask

Can we use == to compare boolean?

Thus, it is safe to say that . equals() hinders performance and that == is better to use in most cases to compare Boolean .

What is a boolean comparison?

Comparison (Boolean) Operators. With null, the result of a comparison will be true, false, or null. The result of a comparison (except for a comparison of reference variables) is null when one or both operands of the expression are null.

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.

Can you compare strings in boolean?

Using the Strict Equality Operator (===) In this method, we will use the strict equality operator to compare strings to Boolean. The strict equality always returns false when we compare string and boolean values as it also checks for the data type of both operands.


1 Answers

Try this:

if (Boolean.TRUE.equals(yourValue)) { ... } 

As additional benefit this is null-safe.

like image 74
yglodt Avatar answered Oct 10 '22 15:10

yglodt