OPERATOR |
MEANING |
EXPLANATION |
EXAMPLE |
RESULT |
& |
Logical AND |
Produce true if both operands are also true otherwise false |
True & false |
false |
| |
Logical OR |
Produce true if one of them operand is true, otherwise false |
True | false |
true |
^ |
Logical XOR (exclusive OR) |
if exactly one operand is true, then the result is true. Otherwise, the result is false |
True ^ false |
true |
|| |
Short-circuit OR |
Check below program |
Check below program |
– |
&& |
Short-circuit AND |
It will not check the second condition if the first one is false |
Check below program |
– |
! |
Logical unary NOT |
This sign is used for invert the result |
!true |
false |
&= |
AND assignment |
This is a short sign of Produce true if both operands are also true otherwise false on the same operand |
Check below program |
– |
|= |
OR assignment |
This is a short sign of Produce true if one of the operand is true otherwise false on the same operand |
Check below program |
– |
^= |
XOR assignment |
This is a short sign of produce true if exactly one operand is true, otherwise, the result is false on the same operand |
Check below program |
– |
EX.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
public class Boolean_LogicalDemo { public static void main(String[] args) { System.out.println("....Boolean Logical Operators......"); System.out.println("<---------Basic Boolean Logical Operators--------->"); boolean a = false; boolean b = true; boolean c = a | b; //Logical AND operator boolean d = a & b; //Logical OR operator boolean e = a ^ b; //Logical XOR(exclusive OR) operator boolean f = (!a & b) | (a & !b); boolean g = !a; //Logical unary NOT operator System.out.println(" a = " + a); System.out.println(" b = " + b); System.out.println("Boolean Logical OR : a | b = " + c); System.out.println("Boolean Logical AND : a & b = " + d); System.out.println("Boolean Logical XOR(exclusive OR) : a ^ b = " + e); System.out.println("(!a & b) | (a & !b) = " + f); System.out.println("!a = " + g); System.out.println(); System.out.println("<---------Boolean Logical Assignment Operators--------->"); System.out.println("Before apply assignment operator, the value of a = "+a); a &= b; // System.out.println("After apply assignment operator : a &= b means a = a & b hence now the value of a : "+a); /*Same as you can check Boolean OR assignment,Boolean exclusive OR assignment, Boolean Equal to and Boolean Not equal to */ } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public class Boolean_ShortCircuitDemo { public static void main(String[] args) { System.out.println("<---------Boolean Short-Circuit Operators--------->"); int i = 10; int j = 20; if (i != 10 && ++j/i>0) { System.out.println("i = "+i); System.out.println("j = "+j); } System.out.println("In Short-circuit AND operator(&&): "); System.out.println("Not checked the second condition in IF-BLECK if first condition is false........."); System.out.println("So,the value of j = "+j); //Same as you can check Short-circuit OR operator(||) } } |