‹‹ Previous
Next ››
Java Operators
Logical Operators
The Boolean logical operators work on boolean operands. In the case of short-circuit logical operators || (OR) and && (AND), Java does not bother to evaluate the right-hand operand
when the outcome of the expression can be determined by the left operand alone.
Operator | Purpose |
&
| Logical AND
|
|
| Logical OR
|
^
| Logical XOR (exclusive OR)
|
!
| Logical unary NOT
|
&=
| AND assignment
|
|=
| OR assignment
|
^=
| XOR assignment
|
!=
| Not equal to
|
==
| Equal to
|
&&
| Short-circuit AND
|
||
| Short-circuit OR
|
?:
| Ternary if-then-else
|
Logical Operator Example 1
Example 1 is demostrating logical operators &, |, ^ and !.
class BoolOp1 {
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 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 = " + g);
}
}
Output of the program
a = true
b = false
a|b = true
a&b = false
a^b = true
!a = false
Logical Operator Example 2
Example 2 is demostrating logical assignment operators &=, |=, ^= ,!= and ==.
class BoolOp2 {
public static void main(String args[]) {
boolean a = true;
boolean b = false;
System.out.println(" Value of a is " + a);
System.out.println(" Value of b is " + b);
a |= b;
System.out.println(" Result of a (a |= b) is " + a);
a &= b;
System.out.println(" Result of a (a &= b) is " + a);
a ^= b;
System.out.println(" Result of a (a ^= b) is " + a);
boolean c = a!=b;
System.out.println(" Result of c (c = a!=b) is " + c);
boolean d = a==b;
System.out.println(" Result of d (d = a==b) is " + d);
}
}
Output of the program
Value of a is true
Value of b is false
Result of a (a |= b) is true
Result of a (a &= b) is false
Result of a (a ^= b) is false
Result of c (c = a!=b) is false
Result of d (d = a==b) is true
Short-Circuit Logical Operators
In case of short-circuit logical operators || (OR operator) and && (AND operator), Java compiler does not evaluate right-hand expression
if the outcome can be determined by left-hand expression only.
For example
if (x != 0 && y > 5)
In this example, if value of x = 0 then the first expression (x != 0) will be false and then the outcome of total expression will be false.
Becuase of short-circuit && (AND) operator, JAVA does not evaluate the second expression if the outcome of total expression can be determined by the first
expression.
Short-circuit && (AND) operator can be used to avoid run-time exception. For exanple, it can be used to avoid 'Divide-By-Zero' runtime exception.
For Example:
if (x != 0 && y/x > 5)
In this example, if x = 0 then the first expression will be false and Java will not evaluate the second expression, therefore, it will avoid evaluting the
expression y/x (Hence, it avoids 'Divide By Zero' runtime exception).
If the same example is used with single & (AND) operator, as given below:
if (x != 0 & y/x > 5)
Becuase of single & (AND) operator, both expressions are evaluted by JAVA. It will cause 'Divide By Zero' runtime exception in the case of x = 0.
‹‹ Previous
Next ››