Skip to content

Logical Operators in Java

Overview

The operators available in Java are shown in the table below.

NameUsageEvaluates to
ANDa && btrue if a and b are both true, otherwise false
ORa || btrue if a is true or b is true (or both), otherwise false
NOT!athe opposite of a (false if a is true; true if a is false)

Using the operators

Here are some examples of using the operators in Java:

java
public class Main {
  public static void main(String[] args) {
        boolean isBookAvailable = false;
        boolean hasOverdueBooks = true;

        System.out.println(!isBookAvailable); // NOT
        System.out.println(isBookAvailable && hasOverdueBooks); // AND
        System.out.println(isBookAvailable || hasOverdueBooks); // OR
    }
}
console
true
false
true