Logic Building is vital in programming to create simple as well as complex algorithms appropriately. This functionality is achieved via various elements such as the logical operators in Java. This operator is effective, especially for the programmer to negate the outcome in the case of eliminating the returned garbage values or returning an elsewise outcome.
This article will demonstrate applying the logical not “!” operator in Java.
What is Meant by ! in Java?
The “!” corresponds to the logical not operator that reverses the result and retrieves “false” if the result/outcome is “true”.
Syntax
!(operand)
In this syntax, “operand” indicates the expression to be evaluated.
Example 1: Applying the Logical Not ! Operator
In this code, the logical not ! operator will be applied to negate the outcome after the defined value is checked for being in a certain range:
public class Logicalnot {
public static void main(String[] args) {
int x = 5;
System.out.println(!(x < 3 && x > 7));
}}
In this snippet of code, initialize the integer. After that, apply the logical not ! operator to negate the applied condition via the logical && operator, thereby satisfying the expression.
Output

This Boolean output verifies that although the defined integer does not satisfy both the ranges, the applied logical not ! operator reverses the outcome and so the value “true” is retrieved.
However, the removal of the logical not ! operator returns the Boolean value “false” as the condition becomes unsatisfied:

Example 2: Applying the Logical Not ! Operator With “if-else” Statements
This demonstration applies the discussed operator with the “if-else” statement to invoke the corresponding condition instead of a Boolean value:
public class Logicalnot {
public static void main(String[] args) {
int x = 5;
if (!(x < 3 && x > 7))
System.out.println("Logical not operator Effective!");
else
System.out.println("Logical not operator ineffective!");
}}
According to this block of code:
- Initialize the integer to be analyzed.
- Now, apply a condition upon the defined integer via the logical && operator such that it lies in the provided range.
- Also, associate the logical not ! operator with the expression to negate the outcome.
- It is such that upon the condition evaluated as “true” after the applied logical not ! operator, the “if” statement executes.
- Else, the “else” statement is invoked.
Output

This output implies that the “if” statement is invoked, making the resultant condition as “true”.
Example 3: Limitations of the Logical Not ! Operator
This specific operator cannot be applied upon an expression returning a value other than Boolean.
The below-provided code demonstrates the discussed concept:
public class Notequal {
public static void main(String args[]) {
int x = 2, y = 3;
System.out.println("Resultant Outcome -> " + !(x + y));
}}
In these code lines, perform the following steps:
- Likewise, define the stated integers to be evaluated.
- Now, specify the numeric expression (x+y) that returns an integer value.
- Since, “!” corresponds to a unary operator and it expects a Boolean but the expression i.e., (x+y) evaluates to int, therefore, this code returns the compilation error.
Output

As seen, the limitation is encountered upon the returned outcome against the expression being “int”.
Conclusion
The “!” corresponds to the logical not operator in Java that reverses the result and gives “false” if the result is “true”. This operator returns an exception if the outcome to be returned appears to be a data type other than a Boolean.