Cleanest way to toggle a boolean variable in Java?

Kevin Griffin picture Kevin Griffin · Oct 22, 2008 · Viewed 158.7k times · Source

Is there a better way to negate a boolean in Java than a simple if-else?

if (theBoolean) {
    theBoolean = false;
} else {
    theBoolean = true;
}

Answer

Aaron Maenpaa picture Aaron Maenpaa · Oct 22, 2008
theBoolean = !theBoolean;