0

Hi guys,

I am using boolean variables as flags in a program and i want to be able to invert the value.

for example if the current value is true, then it must be changed to false; doesn't matter if i don't know what the current value is.

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by redZERO
2

Hi guys,

I am using boolean variables as flags in a program and i want to be able to invert the value.

for example if the current value is true, then it must be changed to false; doesn't matter if i don't know what the current value is.

Use the ! operator.

boolean a = true;
        System.out.println (Boolean.toString(a)); // true
        a = !a;
        System.out.println (Boolean.toString(a)); // false
Votes + Comments
Yes!
A no nonsense answer in like 1 minute
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.