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
?
Thus, it is safe to say that . equals() hinders performance and that == is better to use in most cases to compare Boolean .
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.
boolean isEqual = Boolean. equals(bool1, bool2); which should return false if they are not equal, or true if they are.
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.
Try this:
if (Boolean.TRUE.equals(yourValue)) { ... }
As additional benefit this is null-safe.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With