Logical Operators are used in conditional statements and loops for evaluating a condition with binary values.
All of the binary logical operators combine two boolean values that are true and false to form a result value.
Logical Operators with their description:
Operator | Description | Example |
---|---|---|
&& (logical and) | If both the operands are non-zero, then the condition becomes true. | (X && Y) is false |
|| (logical or) | If any of the two operands are non-zero, then the condition becomes true. | (X || Y) is true |
! (logical not) | Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(X && Y) is true |
Java Program for Logical Operator
1 2 3 4 5 6 7 8 9 10 11 12 | public class Logical { public static void main(String args[]) { boolean x = true; boolean y = false; System.out.println("&& (logical and) = " + (x&&y)); System.out.println("|| (logical or) = " + (x||y) ); System.out.println("! (logical not) = " + !(x&&y)); } } |
Output:
1 2 3 | && (logical and) = false || (logical or) = true ! (logical not) = true |