If you have a boolean variable:
boolean myBool = true;
I could get the inverse of this with an if/else clause:
if (myBool == true) myBool = false; else myBool = true;
Is there a more concise way to do this?
The ! is a logical operator that will convert a value to its opposite boolean. Since JavaScript will coerce values, it will “convert” a value to its truthy/falsey form and return the opposite boolean value. When we perform the ! operation on a number other than 0 it, returns false .
You can change your return type to boolean[] so that you can return 2 boolean values. Show activity on this post. "return" terminates your method and returns the value. Everything below return is not reachable.
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 .
Just assign using the logical NOT operator !
like you tend to do in your condition statements (if
, for
, while
...). You're working with a boolean value already, so it'll flip true
to false
(and vice versa):
myBool = !myBool;
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