42

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?

Joe Doyle
  • 6,363
  • 3
  • 42
  • 45
faq
  • 757
  • 2
  • 8
  • 9
  • 3
    BoltClock has the optimal answer; I'd like to note though, that "if (myBool == true)" is by itself already very chatty; "if (myBool)" does exactly the same thing. – phisch Oct 11 '10 at 15:11
  • 1
    @phisch: `if ((!myBool != true || false) && (!(myBool != false || true) == false))` – BoltClock Oct 11 '10 at 15:17
  • 1
    myBool = (myBool) ? false : true; (because I just can't resist the opportunity to use the ternary form even if this isn't - the most - concise) – KevinDTimm Oct 11 '10 at 15:20
  • 2
    He forgot the braces. if(myBool == true) { myBool = false; } ... :-) – James Schek Oct 11 '10 at 15:40
  • 1
    `if (myBool == true)`, eek. Related: [Is it bad to explicitly compare against boolean constants e.g. if (b == false) in Java?](http://stackoverflow.com/questions/2661110/is-it-bad-to-explicitly-compare-against-boolean-constants-e-g-if-b-false-in) – BalusC Oct 11 '10 at 16:06
  • by the way, the object oriented version xD `myBool = Boolean.FALSE.equals(myBool);` – fortran Feb 19 '13 at 14:50

5 Answers5

106

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;
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 2
    This is indeed more concise. But I would like to point out that the answer from `bvdb` deals with properly handling `Boolean` – buzzsawddog Jul 02 '14 at 20:27
45

An even cooler way (that is more concise than myBool = !myBool for variable names longer than 4 characters if you want to set the variable):

myBool ^= true;

And by the way, don't use if (something == true), it's simpler if you just do if (something) (the same with comparing with false, use the negation operator).

fortran
  • 74,053
  • 25
  • 135
  • 175
  • 12
    Heck yeah XOR! +1 – BoltClock Oct 11 '10 at 15:13
  • 8
    Those are the little things that make the endless hours of programming funnier xD – fortran Oct 11 '10 at 15:14
  • 5
    I was hoping to find a concise syntax that my coworkers will understand intuitively without needing to ask a question on StackOverflow to understand what I'm doing. – faq Oct 11 '10 at 15:14
  • 3
    You are not thinking in positive! Think about the enlightenment they will get after browsing SO for a while trying to understand what you were doing!!! :-p – fortran Oct 11 '10 at 15:16
  • 1
    You're right. Let's see what they think of our efforts to enlighten them. – faq Oct 11 '10 at 15:21
  • They will be delighted, I can tell you that! :-D – fortran Oct 11 '10 at 15:21
  • 3
    how about `myBool = (myBool == false);`? – Jorn Oct 11 '10 at 15:29
  • 35
    @faq: You're completely missing the point! The goal of programming isn't to write programs that others can understand. It's to write programs that are so sophisticated that anyone reading it is instantly impressed with how smart you must have been to be able to write this. :) – Jay Oct 11 '10 at 16:19
  • 8
    @Jay or as a friend of mine would say: If it was hard to write, it should be hard to read! XD – fortran Oct 13 '10 at 07:08
  • What are the benefits of the bitwise XOR method? (Other than coolness). – Usman Ismail Apr 09 '14 at 18:57
  • 1
    no benefits, just for bugging your fellows... (and it's not bitwise, it's a boolean operator since it's applied on boolean operands!) – fortran Apr 10 '14 at 02:40
  • 1
    This solution is neat. To say `myBool = !myBool` allows you to only exactly flip the boolean value. But, `myBool ^= true` can be generalised as `myBool ^= reverse` which allows you to selectively invert its boolean value. – Paul Schwarz Sep 01 '18 at 21:35
19

For a boolean it's pretty easy, a Boolean is a little bit more challenging.

  • A boolean only has 2 possible states: trueand false.
  • A Boolean on the other hand, has 3: Boolean.TRUE, Boolean.FALSE or null.

Assuming that you are just dealing with a boolean (which is a primitive type) then the easiest thing to do is:

boolean someValue = true; // or false
boolean negative = !someValue;

However, if you want to invert a Boolean (which is an object), you have to watch out for the null value, or you may end up with a NullPointerException.

Boolean someValue = null;
Boolean negativeObj = !someValue.booleanValue(); --> throws NullPointerException.

Assuming that this value is never null, and that your company or organization has no code-rules against auto-(un)boxing. You can actually just write it in one line.

Boolean someValue = Boolean.TRUE; // or Boolean.FALSE
Boolean negativeObj = !someValue;

However if you do want to take care of the null values as well. Then there are several interpretations.

boolean negative = !Boolean.TRUE.equals(someValue); //--> this assumes that the inverse of NULL should be TRUE.

// if you want to convert it back to a Boolean object, then add the following.
Boolean negativeObj = Boolean.valueOf(negative);

On the other hand, if you want null to stay null after inversion, then you may want to consider using the apache commons class BooleanUtils(see javadoc)

Boolean someValue = null; // or Boolean.TRUE or Boolean.FALSE;
Boolean negativeObj = BooleanUtils.negate(someValue);

Some prefer to just write it all out, to avoid having the apache dependency.

Boolean someValue = null; // or Boolean.TRUE or Boolean.FALSE;
Boolean negative = (someValue == null)? null : Boolean.valueOf(!someValue.booleanValue());
bvdb
  • 22,839
  • 10
  • 110
  • 123
  • 1
    Thank you for this answer. I feel the accepted answer is indeed more concise. BUT I feel your answer is more complete! Thank you for covering `boolean` AND `Boolean` – buzzsawddog Jul 02 '14 at 20:26
  • I gave you thumb up, but just to put a note on Boolean negation - the way you did it causes 'The expression of type Boolean is unboxed into boolean' – horvoje Mar 29 '22 at 11:38
  • @horvoje some code inspections prefer auto-(un)boxing, others forbid it. You can disable/enable the inspections in the configuration of your IDE, depending on your personal preference. Personally, I would avoid auto-unboxing until you really understand the implications for null-values. – bvdb Nov 07 '22 at 08:04
1

The most concise way is to not invert the boolean, and just use !myBool later on in the code when you want to check the opposite condition.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
-3
myBool = myBool ? false : true;
Florent
  • 12,310
  • 10
  • 49
  • 58
Jinjavacoder
  • 267
  • 1
  • 4
  • 13