Logical operators are generally used with Boolean values and after operation they also return a Boolean value. Sometimes these operators are used with non-Boolean values then they return non-Boolean values.
These operators show the control program flow. Let's take two Boolean values "a" and "b", here "a" holds TRUE and "b" holds FALSE.
A list of logical operators in Java language:
S.No. | Operators | Example | Description |
---|---|---|---|
1. | && (Logical AND) | (a && b) is False | It is called logical AND operator. It specifies TRUE, if both the operands are non-zero. |
2. | || (Logical OR) | (a || b) is TRUE | It is called logical OR operator. It specifies TRUE, if any of the two operands are non-zero. |
3. | ! (Logical NOT) | !(a && b) is TRUE | It is called logical NOT operator. It is used to reverses the logical state of its operand. If a condition is true then logical NOT operator will make it FALSE. |
The following program is a simple example that demonstrates the logical operators.
/** * * @author JavaTportal Corporation * */ public class LogicalOperatorsExample { public static void main(String args[]) { boolean a = true; boolean b = false; System.out.println("a && b = " + (a && b)); System.out.println("a || b = " + (a || b)); System.out.println("!(a && b) = " + !(a && b)); } }
a && b = false a || b = true !(a && b) = true