Java Tutorials : Java Operators &
Control Flow Statement Explained
5. Bit-wise operators - &, ^, |
- Operate on numeric and Boolean operands.
- & - AND operator, both bits must be 1 to produce
- - OR operator, any one bit can be 1 to produce 1.
- ^ - XOR operator, any one bit can be 1, but not
both, to produce 1.
- In case of Booleans true is 1, false is 0.
- Can't cast any other type to Boolean.
6. Short-circuit logical operators - &&
- Operate only on Boolean types.
- RHS might not be evaluated (hence the name short-circuit),
if the result can be determined only by looking at LHS.
- False && X is always false.
- True || X is always true.
- RHS is evaluated only if the result is not certain
from the LHS.
- That's why there's no logical XOR operator. Both
bits need to be known to calculate the result.
- Short-circuiting doesn't change the result of the
operation. But side effects might be changed. (i.e.
some statements in RHS might not be executed, if short-circuit
happens. Be careful)
7. Ternary operator
- Format a = x ? b : c ;
- x should be a Boolean expression.
- Based on x, either b or c is evaluated. Both are
- b will be assigned to a if x is true, else c is
assigned to a.
- b and c should be assignment compatible to a.
- b and c are made identical during the operation
according to promotions.