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.

Recommended Answers

All 2 Replies

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.

boolean a = true;
        System.out.println (Boolean.toString(a)); // true
        a = !a;
        System.out.println (Boolean.toString(a)); // false
commented: A no nonsense answer in like 1 minute +2
commented: Yes! +8

Excellent, thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.