DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   Inverting value of boolean variable? (http://www.daniweb.com/forums/thread196022.html)

redZERO Jun 5th, 2009 3:30 pm
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.

VernonDozier Jun 5th, 2009 3:38 pm
Re: Inverting value of boolean variable?
 
Quote:

Originally Posted by redZERO (Post 884463)
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

redZERO Jun 5th, 2009 3:48 pm
Re: Inverting value of boolean variable?
 
Excellent, thank you very much!


All times are GMT -4. The time now is 2:41 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC