4. Java Operators

Logical operators

  • Logical operators are used to combine two or more simple conditions to create more complex conditions.
  • There are three logical operators in Java.
  • These operators are similar in function to logic gates AND, OR , NOT.
OperatorOperationExplanation
&&Logical ANDIf both conditions on the left and right are satisfied, outputs true.
||Logical ORIf one of either left or right conditions are satisfied, outputs true.
!Logical NOTInverts the output of the provided condition. (If condition is ‘true’, then outputs ‘false’ or vice versa.)

// AND
i == j && l == m

// OR
i > j || 1 != m

// NOT
!(i <= j)
    
  • Also multiple operators can be used simultaneously.

(i == j && n == m) || (i > j && !(m <= j))
    

E.g.:


int a = 10;
int b = 12;

System.out.println((a == b) && (a < b))
    
false
    

Which of the following logical operator is equivalent to the "OR" logic gate.

Pages: 1 2 3 4 5 6 7