Logical Expressions

   

Logical expressions work a bit differently than the previous operators and are probably not something you covered in your third-grade math class. These expressions either operate on a pair of Booleans or manipulate the individual bits of integer variables or literals. This distinction divides the logical operators into two categories:

  • Boolean operators ” Only operate on boolean operands.

  • Bitwise operators ” Operate on each bit in a pair of integer operands.

You have already seen in Chapter 5, "Using Expressions," how bitwise operators work. This chapter covers only the Boolean, or conditional, half of the logical expression operators. However, it is interesting to note that, with some minor exceptions, bitwise operators and conditional operators produce the same result if the operands are boolean.

Conditional-AND and Conditional-OR Operators

There are two primary Boolean operators:

  • Conditional-AND, &&

  • Conditional-OR,

These operators obey the same truth table that was constructed in Chapter 5 for the bitwise operators, but have lower precedence. For your convenience, the truth tables for AND and OR are reproduced here:

When A is And B is (A && B) (A B)
false false false false
false true false true
true false false true
true true true true

These operators provide an additional feature related to efficiency. The operands of a conditional-OR or a conditional-AND expression are evaluated left-to-right ; if the value of the expression can be determined after evaluating only the left operand, the right operand will not be evaluated. So, in the following example, if x is indeed less than y, then m and n are not compared:

 (x<y)  (m>n) 

If the left side of this expression produces the Boolean value true, then the result of the whole expression is true, regardless of the result of the comparison m>n. The right operand is only evaluated if the left operand is false. This behavior is the reason and && are referred to as "conditional" operators. Note that in the following expression, if you instead used the bitwise form of the operator, m and n are compared regardless of the values of x and y:

 (x<y)  (m>n) 

The && operator behaves similarly to . If the left operand of a conditional-AND expression is false, then the result of the expression is obviously false. The right operand is only evaluated if the left operand evaluates to true.

The conditional-AND and conditional-OR operators are often referred to as short-circuit operators because they short-circuit the evaluation of the right operand if it's unnecessary. You should normally use them instead of the bitwise form. The only exception would be cases in which the right operand produces a side effect by calling a method or applying another operator that changes the value of a variable or does some other work. Here you should use the bitwise form to make sure the work of the right operand is performed.

Note

You might have noticed that there is no conditional-XOR operator defined as part of this set. This is because both sides of an XOR comparison must always be evaluated to determine the result of an expression.


The Logical Negation Operator

The logical negation, or NOT, operator (!), is a unary operator that evaluates the opposite value of a Boolean expression. For example, !true evaluates to false. Recall that the bitwise complement operator (~) described in Chapter 5 cannot be applied to a Boolean expression. You must use the negation operator to obtain an opposite value when working with Booleans.

The negation operator has high precedence, equivalent to that of the other unary operators. Take a look at the following example, which shows a combination of logical negation and the conditional-AND:

 if (!done && inputString.equals("exit") ) 

Here, assume done is a boolean variable that indicates that some required processing has finished. Because the logical negation operator has higher precedence, it is evaluated before the conditional-AND. The method call in the right operand is only performed if done is false in this example. This is a good use of a short-circuit operator, especially if this statement is inside a loop or a method that gets called often because string comparisons are relatively slow.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

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