Inverting value of boolean variable?

Thread Solved
Reply

Join Date: Dec 2007
Posts: 74
Reputation: redZERO is an unknown quantity at this point 
Solved Threads: 2
redZERO redZERO is offline Offline
Junior Poster in Training

Inverting value of boolean variable?

 
0
  #1
Jun 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 494
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Inverting value of boolean variable?

 
2
  #2
Jun 5th, 2009
Originally Posted by redZERO View Post
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.

  1. boolean a = true;
  2. System.out.println (Boolean.toString(a)); // true
  3. a = !a;
  4. System.out.println (Boolean.toString(a)); // false
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 74
Reputation: redZERO is an unknown quantity at this point 
Solved Threads: 2
redZERO redZERO is offline Offline
Junior Poster in Training

Re: Inverting value of boolean variable?

 
0
  #3
Jun 5th, 2009
Excellent, thank you very much!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC