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.
Operator | Operation | Explanation |
---|---|---|
&& | Logical AND | If both conditions on the left and right are satisfied, outputs true. |
|| | Logical OR | If one of either left or right conditions are satisfied, outputs true. |
! | Logical NOT | Inverts 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.