Java Logical Operators

The Java Logical Operators work on the Boolean operand. It's also called Boolean logical operators. It operates on two Boolean values, which return Boolean values as a result.

OperatorMeaningWork
&&Logical ANDIf both operands are true then only "logical AND operator" evaluate true.
||Logical ORThe logical OR operator is only evaluated as true when one of its operands evaluates true. If either or both expressions evaluate to true, then the result is true.
!Logical NotLogical NOT is a Unary Operator, it operates on single operands. It reverses the value of operands, if the value is true, then it gives false, and if it is false, then it gives true.

Program to Show Logical Operators Works

Example:
public class logicalop {
 public static void main(String[] args) {
  //Variables Definition and Initialization
  boolean bool1 = true, bool2 = false;

  //Logical AND
  System.out.println("bool1 && bool2 = " + (bool1 && bool2));
  
  //Logical OR
  System.out.println("bool1 || bool2 = " + (bool1 | bool2) );
  
  //Logical Not
  System.out.println("!(bool1 && bool2) = " + !(bool1 && bool2));

 }
}
Output:
bool1 && bool2 = false
bool1 || bool2 = true
!(bool1 && bool2) = true

Scroll Back to Top