954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Inverting value of boolean variable?

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.

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 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
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Excellent, thank you very much!

redZERO
Junior Poster in Training
82 posts since Dec 2007
Reputation Points: 10
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You