The Boolean logical operators operate only on boolean operands.
Boolean Logical Operators
| Operator | Result |
|---|---|
| & | Logical AND |
| | | Logical OR |
| ^ | Logical XOR (exclusive OR) |
| || | Short-circuit OR |
| && | Short-circuit AND |
| ! | Logical unary NOT |
| &= | AND assignment |
| |= | OR assignment |
| ^= | XOR assignment |
| == | Equal to |
| != | Not equal to |
| ? : | Ternary if-then-else |
The following table shows the effect of each logical operation:
Logical Operations
| A | B | A | B | A & B | A ^ B | !A |
|---|---|---|---|---|---|
| False | False | False | False | False | True |
| True | False | True | False | True | False |
| False | True | True | False | True | True |
| True | True | True | True | False | False |
The following program demonstrates the boolean logical operators.
public class Main {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a & !b);
boolean g = !a;
System.out.println(" a = " + a);
System.out.println(" b = " + b);
System.out.println(" a|b = " + c);
System.out.println(" a&b = " + d);
System.out.println(" a^b = " + e);
System.out.println("!a&b|a&!b = " + f);
System.out.println(" !a = " + g);
}
}
The output:
a = true
b = false
a|b = true
a&b; = false
a^b = true
!a&b;|a&!b = true
!a = falsejava2s.com | | Contact Us | Privacy Policy |
| Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
| All other trademarks are property of their respective owners. |