Using The Not Operator In Java If Conditions

Java’s if-else statement allows us to execute a section of code based on a condition. The if block is executed if its condition evaluates to true and the else block is executed if the condition evaluates to false.

Java If Else Statement

Let’s examine the simple if-else statement in Java by looking at its syntax.

1
2
3
4
5
if (expression) {
    //execute when expression evaluates to true
} else {
    //execute when the expression evaluates to false
}

Above the expression is a boolean expression that evaluates to either true or false.

If the expression evaluates to true the statements within the block of the if statement are executed. Otherwise, if the expression evaluates to false, statements inside the body of the if block are skipped, and the else block is executed.

1
2
3
4
5
6
boolean isDone = true;
if (isDone) {
    System.out.println("Done");
} else {
    System.out.println("Not Done");
}

Above we expect Done to be output to the console as the isDone variable evaluates to true.

The Not Operator

However, what if we just want to handle the case when the if expression evaluates to false.

One way of doing this is to remove the code in the if block:

1
2
3
4
if (isDone) {
} else {
    System.out.println("Not Done!");
}

However, this results ugly looking code that requires more effort to understand and write.

We can improve the code by removing the else block and testing if the expression within the if statement evaluates to false.

1
2
3
if (isDone == false) {
    System.out.println("Not Done!");
}

Although, the above code is better, we can still make it more concise, and improve its readability. This is especially true, where the if statement contains more than a single expression. For example:

1
2
3
if (isDone == false || isActive == false || isError==false ) {
    System.out.println("Not Done!");
}

Not Operator Example

We can improve the above code using the not operator in Java.

1
2
3
if (!isDone) {
    System.out.println("Not Done!");
}

The not operator is known as a logical operator. It is represented in Java as shown above using the ! symbol. The not operator is a unary operator – it takes a single operand, a boolean value. The not operator works by inverting the the boolean value.

For example:

1
2
3
System.out.println(!true);   // prints false 
System.out.println(!false);  // prints true 
System.out.println(!!false); // prints false

Using the Not Operator With Boolean Expressions

As mentioned not is a unary operator therefore, when we apply it to a compound boolean expression or boolean expressions that use their own Java operators, we must surround those expressions with parenthesis. In this case we create a parenthesized expression, that instructs the compiler to execute before what is outside.

For example:

1
2
3
4
5
6
final boolean x = true;
final boolean y = false;

System.out.println(!(x && y));  // prints true
System.out.println(!(x || y));  // prints false
System.out.println(!(x || !y));  // prints false
1
2
3
final int ordinal = 10;
System.out.println(!(ordinal > 10));  // prints true
System.out.println(!(ordinal <= 10)); // prints false

Not Operator And Complex Conditions

Sometimes we may create complex conditions that are hard to read, or where the readability of the code can be improved. For example:

1
2
3
4
5
6
7
8
9
final boolean isWating = true;
final boolean isError = false;
final boolean isProcessed = false;
final boolean isBlocked = false;
final boolean timeout = true;

if((isError || isProcessed) || ((isWating || isBlocked) && timeout)) {
    System.out.println("Terminate");
}

Although, the above example uses descriptive variable names, the reader must read each expression to understand it. We could simplify it, improving readability, by extracting a method with a descriptive name.

1
2
3
public static boolean isTerminated() {
    return (isError || isProcessed) || ((isWating || isBlocked) && timeout);
}

Conclusion

In this tutorial we discussed the not operator, and how it can be used with Java if-else statements and boolean expressions.