Kevin Griffin Kevin Griffin - 1 month ago 9
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
theBoolean = !theBoolean;
Comments