Kevin Griffin Kevin Griffin - 1 year ago 68
Java Question

Cleanest way to toggle a boolean variable in Java?

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

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

Answer Source
theBoolean = !theBoolean;