Java NOT

Java NOT Operator is used to invert the value of a boolean value.

NOT Operator Symbol

The symbol used for NOT Operator is !.

Syntax

The syntax to use NOT Operator with an operand a is

</>
Copy
!a

a can be a Boolean variable, or boolean expression, or a complex condition.

NOT Truth Table

The following truth table provides the output of NOT operator for different values of operands.

a!a
truefalse
falsetrue

NOT Operator negates the given value.

Examples

In the following example, we take a boolean variable with different values: true and false, and find their logical NOT output.

Main.java

</>
Copy
public class Main {
    public static void main(String[] args) {
        boolean a, result;
        
        a = true;
        result = !a;
        System.out.println("!" + a + "  = " + result);
        
        a = false;
        result = !a;
        System.out.println("!" + a + " = " + result);
    }
}

Output

!true  = false
!false = true

In the following example, we will use NOT operator for a boolean condition in If Statement.

index.html

</>
Copy
public class Main {
    public static void main(String[] args) {
        int a = 4;
        if (!(a % 2 == 1)) {
            System.out.println("a is not odd.");
        } else {
            System.out.println("a is odd.");
        }
    }
}

Output

a is not odd.

Conclusion

In this JavaScript Tutorial, we learned about Logical NOT Operator, its syntax, and usage with examples.