Friday, 15 September 2017

Java Conditional And (&&)/OR (||) Operator vs. Boolean Logical And(&)/Logical Inclusive OR(I) Operator

Cited from the Book Java How to Program

The boolean logical AND (&) and boolean logical inclusive OR (|) operators are identical to the && and || operators, except that the & and | operators always evaluate both of their operands (i.e., they do not perform short-circuit evaluation).

This is useful if the right operand of the boolean logical AND or boolean logical inclusive OR operator has a required side effect—a modification of a variable’s value.

For example, the expression

( birthday == true ) | ( ++age >= 65 )

guarantees that the condition ++age >= 65 will be evaluated. Thus, the variable age is incremented, regardless of whether the overall expression is true or false.

No comments:

Post a Comment