3.11 Boolean Logical Operators: , , ,


3.11 Boolean Logical Operators: ! , ^ , & ,

Boolean logical operators include the unary operator ! ( logical complement ) and the binary operators & ( logical AND), ( logical inclusive OR), and ^ ( logical exclusive OR, a.k.a. logical XOR). Boolean logical operators can be applied to boolean operands, returning a boolean value. The operators & , , and ^ can also be applied to integral operands to perform bitwise logical operations (see Section 3.13, p. 75).

Given that x and y represent boolean expressions, the boolean logical operators are defined in Table 3.9. In the table, the operators are ranked, with the operator having the highest precedence first.

Table 3.9. Boolean Logical Operators

Logical complement

!x

Returns the complement of the truth-value of x.

Logical AND

x & y

true if both operands are true; otherwise , false .

Logical OR

x y

true if either or both operands are true; otherwise, false .

Logical XOR

x ^ y

true if and only if one operand is true; otherwise, false .

These operators always evaluate both the operands, unlike their counterpart conditional operators && and (see Section 3.12, p. 72). Truth-values for boolean logical operators are shown in Table 3.10.

Table 3.10. Truth-values for Boolean Logical Operators

x

y

!x

x & y

x y

x ^ y

true

true

false

true

true

false

true

false

false

false

true

true

false

true

true

false

true

true

false

false

true

false

false

false

Operand Evaluation for Boolean Logical Operators

In evaluation of boolean expressions involving boolean logical AND, XOR, and OR operators, both the operands are evaluated. The order of operand evaluation is always from left to right.

 if (i > 0 & i++ < 10) {/*...*/} // i will be incremented, regardless of value in i. 

The binary boolean logical operators have precedence lower than arithmetic and relational operators, but higher than assignment, conditional AND, and OR operators (see Section 3.12, p. 72). This is illustrated in the following examples:

 boolean b1, b2, b3 = false, b4 = false; b1 = 4 == 2 & 1 < 4;           // false, evaluated as (b1 = ((4 == 2) & (1 < 4))) b2 = b1  !(2.5 >= 8);         // true b3 = b3 ^ b2;                  // true b4 = b4  b1 & b2;             // false 

Order of evaluation is illustrated for the last example:

 (b4 = (b4  (b1 & b2)))  (b4 = (false  (b1 & b2)))  (b4 = (false  (false & b2)))  (b4 = (false  (false & true)))  (b4 = (false  false))  (b4 = false) 

Note that b2 was evaluated although, strictly speaking, it was not necessary. This behavior is guaranteed for boolean logical operators.

Boolean Logical Compound Assignment Operators: &= , ^= , =

Compound assignment operators for the boolean logical operators are defined in Table 3.11. The left-hand operand must be a boolean variable, and the right-hand operand must be a boolean expression. An identity conversion is applied implicitly on assignment. These operators can also be applied to integral operands to perform bitwise compound assignments (see Section 3.13, p. 78).

Table 3.11. Boolean Logical Compound Assignment Operators

Expression:

Given b and a Are of Type Boolean , the Expression Is Evaluated as:

b &= a

b = (b & (a))

b ^= a

b = (b ^ (a))

b = a

b = (b (a))

Examples of Boolean Logical Compound Assignment
 boolean b1 = false, b2 = false, b3 = false; b1 = true;                     // true b2 ^= b1;                       // true b3 &= b1  b2;                  // (1) false. b3 = (b3 & (b1  b2)). b3 = b3 & b1  b2;              // (2) true.  b3 = ((b3 & b1)  b2). 

It is instructive to note how the assignments at (1) and (2) above are performed, giving different results for the same value of the operands.



A Programmer[ap]s Guide to Java Certification
A Programmer[ap]s Guide to Java Certification
ISBN: 201596148
EAN: N/A
Year: 2003
Pages: 284

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net