Java Logical NOT Operator

Introduction

In this lesson of Java course, we will look at Java logical NOT operator. In Java, logical operators are used to create more complex logical expressions that evaluate to true or false. The Java logical NOT operator is a unary operator that takes a single boolean operand and returns the opposite of its value. It is denoted by the exclamation mark (!) and is used with other logical operators to create more complex expressions.

1. Syntax

The syntax for the logical NOT operator is:

!boolean_expression

The boolean_expression can be any expression that evaluates to a boolean value. The logical NOT operator returns true if the boolean_expression is false, and false if the boolean_expression is true.

2. Examples

Here are some examples that illustrate the use of the Java logical NOT operator:

boolean a = true;
boolean b = false;

System.out.println(!a); // Output: false
System.out.println(!b); // Output: true

int x = 10;
int y = 20;

System.out.println(!(x > y)); // Output: true

In the first two examples, the logical NOT operator is used to reverse the logical state of the boolean variables a and b. The output for !a is false because the value of a is true, and the output for !b is true because the value of b is false.

In the third example, the logical NOT operator is used with the greater than (>) operator to create a more complex logical expression that evaluates to true. The expression !(x > y) evaluates to true because the value of x is not greater than the value of y.

3. Precedence

In Java, the logical NOT operator has a higher precedence than other logical operators, such as && and ||. This means that the logical NOT operator is evaluated first, before other logical operators. For example:

boolean a = true;
boolean b = false;
boolean c = true;

System.out.println(!a && b); // Output: false
System.out.println(!(a && b)); // Output: true
System.out.println(!a || !b && c); // Output: true

In the first example, the logical NOT operator is evaluated first, so the expression !a is evaluated first, resulting in false. The expression false && b then evaluates to false.

In the second example, the logical AND operator is evaluated first, so the expression a && b is evaluated first, resulting in false. The logical NOT operator is then applied to the result, resulting in true.

In the third example, the logical NOT operator is evaluated first, so the expression !a is evaluated first, resulting in false. The logical OR operator is then applied to the result of !a and the expression !b && c. The expression !b evaluates to true, and the expression !b && c evaluates to true because both expressions are true. Finally, the logical OR operator applies to false and true, resulting in true.

Summary

The logical NOT operator is a powerful tool for creating more complex logical expressions in Java. It takes a single boolean operand and returns the opposite of its value. By understanding its syntax and precedence, we can use the logical NOT operator to create sophisticated logical expressions that can help us solve a wide range of problems in our Java programs. The source code for this course is available on our GitHub repository.