Toggle a Boolean Variable in Java

Rashmi Patidar Jan 30, 2023 Aug 26, 2022
Toggle a Boolean Variable in Java

The Boolean is a wrapper class in Java that wraps around boolean primitives. In object-oriented programming, the class is a way to maximize object use instead of primitives.

The class concept aroused the solution to bind boolean or bool primitive values within the Boolean class.

Toggle a Boolean Variable in Java

The Boolean class only has two static values, TRUE and FALSE variables. With the static member variables, the Boolean also holds static methods like valueOf(), parseBoolean, toString, and more.

There are many ways for a user to toggle the values of a Boolean variable in a Java program. The source code block to demonstrate the toggle behavior gets shown below.

import static java.lang.Boolean.TRUE;

public class ToggleBoolean {
    public static void main(String[] args) {
        Boolean aBoolean = TRUE;
        System.out.println("Boolean value : " + aBoolean);
        // first way
        System.out.println("Boolean value with ! operator: " + !aBoolean);
        //second way
        Boolean bool = aBoolean ? false : true;
        System.out.println("Boolean value with ternary operator: " + bool);
        //third way
        System.out.println(aBoolean);
        aBoolean ^= aBoolean;
        System.out.println("Boolean value with ^= operator: " + aBoolean);

        //fourth way
        if (aBoolean) {
            aBoolean = false;
            System.out.println("Boolean value using if condition: " + aBoolean);
        } else
            aBoolean = true;
        System.out.println("Boolean value using else condition: " + aBoolean);
    }
}

Use the Not (!) Operator

The operator ! is also known as the Not operator in Java programming. The function of the operator is to negate the operation.

The use of not makes a negative check at places where needed. The use cases can be in the if-else block, conditional checks in while, do-while loops, ternary operator, and more.

The operator gets used as a programming practice when validations like not null are supposed to get made. The places where the not operator gets used with an equals sign for making it not equal to and then check the condition.

The operator can only get used when the user wants values, like 0 or 1, true or false.

In the above code, the not operator is added along with the Boolean variable to negate its current value. The easiest way is to toggle the Boolean variable in one statement.

Use Ternary Condition

The ternary operator is often known using the ?: operator. The syntax of the ternary operator is expression?expression1: expression2.

The first operator is the ? question mark which gets succeeded by an expression. The expression needs to get evaluated, resulting in either true or false.

If the outcome of expression after the evaluation is true value, the resultant would be expression1. The nesting can also get done in the expression statement.

If the result evaluates a false value, then expression2 gets evaluated. In the above source code, Boolean values get toggled with the ternary operator.

Use Bitwise Exclusive OR (^=) Operator

The assignment operator or bitwise exclusive OR operator compares the first operand’s bit-by-bit values with another.

The bit matching for OR is as below:

  1. If bit 0 gets compared with bit 1: the resultant is 1.
  2. If bit 0 gets compared with bit 0: the resultant is 0.

Timewise, the bitwise OR is the quickest way to toggle a Boolean value. It uses the registers inside the CPU to speed up the evaluation.

Use the if-else Loop

The if-else loop is the traditional way of checking conditions. The condition is handy and widely used.

In the source code above, the if-else loop gets used to toggle the values using block logic.

  1. If the statement evaluates to true, the first block gets executed.
  2. If it evaluates to a false value, then expressions in the second block are evaluated.

The above mentioned are all the possible ways to toggle values. The output block that results in the evaluated value is as below.

The output of the Toggle Boolean source code above:

Boolean value : true
Boolean value with ! operator: false
Boolean value with ternary operator: false
true
Boolean value with ^= operator: false
Boolean value using else condition: true
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.

LinkedIn

Related Article - Java Boolean