• No results found

The not Operator

The final Boolean operator I want to discuss is the not operator, which negates a single Boolean expression. If I say something that was not true, it must be false. Similarly, if I say something that is not false, it must be true. The not operator performs this logic in your code.

There is no truth table for the not operator because it is only performed on a single expression.

Consider the following statement:

Not (it is raining out today).

This example may not sound grammatically correct, but it is programmati- cally correct. The not operator is placed at the beginning of a Boolean expres- sion. The statement above is true when it is not raining out today. If it is raining out today, the statement is false.

Table 3.3 The Truth Table for the exclusive or Operator

EXCLUSIVE OR TRUE FALSE

TRUE FALSE TRUE

FALSE TRUE FALSE

Boolean Operators

Now that you have seen the logic behind the operators, let’s look at the corre- sponding programming syntax. Table 3.4 shows the Boolean operators available in Java.

The & and && operators only differ in that the && will short-circuit when the first Boolean expression is false. To demonstrate, consider the following Boolean expression:

(a > 0) && (a < 1)

If a is not greater than 0, the first part of the expression is false. Because the operation is and, we can now say that the entire expression will be false, no matter what the result of the second expression is. In the case of using the two ampersands &&, the expression will short-circuit and the (a < 1) will not be checked.

There are many situations where short-circuiting is the desired result. For example, consider the following expression:

(x != 0) && (y/x < 1)

If x is 0, then you do not want the second expression to be evaluated because it involves integer division by zero, which causes a runtime exception and will make your program crash. If x is not 0, the first part is true, and the second part must be evaluated to determine the result of the entire Boolean expression. Because we are assured that x is not zero, the division y/x is no problem.

In some situations, you might not want the short-circuit behavior. In the following example, the second part of the expression contains an operation that changes the value of the variable x:

(x != 0) && (x++ > 10)

I consider the preceding statement to be poor programming practice, but it illustrates my point. If x is 0, then the x++ increment will not occur. If you want the x++ to be evaluated in all situations, you would use the single ampersand (&):

(x != 0) & (x++ > 10)

No matter if x is 0 or not, the expression (x++ > 10) will be tested. In other words, the single ampersand guarantees that both Boolean expressions will be checked. The same is true for the oroperator ().

Table 3.4 The Boolean Operators

OPERATOR SYNTAX

short-circuit and &&

and &

short-circuit or ||

or |

exclusive or ^

not !

The or operator also has two versions. The || operator will short-circuit if the first Boolean expression is true. (If the first part of the expression is true, it does not matter what the second part evaluates to. The entire expression will be true.)

For example, the following statement will short-circuit:

int x = 10;

(x > 0) || (x-- != 10)

The previous expression is true because the first part is true. What is x after the code above? Because it short-circuits, x will still be 10. The second part of the Boolean expression is not evaluated.

As with the single ampersand, &, you can use the single | to ensure that an or expression never short-circuits:

int x = 10;

(x > 0) | (x-- != 10)

What is x after the code above? This time, no short-circuiting occurs and x will be decremented to 9. The expression still evaluates to true because true or false is true.

The not operator, !, may be placed at the beginning of any Boolean expres- sion or variable. Consider the following statements:

short a = 10, b = 5; boolean test = !(a > b);

Because a is greater than b, the expression in parentheses is true. The not operator is then applied to true, which results in false (not true equals false); therefore, the value of test is false.

You can combine the Boolean operators to create a more complex Boolean expression. For example, try to determine the result of the following code:

int x = 5, y = 6, z = -3;

boolean b = ((x + 3 > y) ^ (z >= y)) && !(x == 5 | ++x == y);

Pay close attention to the parentheses. If you replace the comparisons with true and false, you get the following logically equivalent statement:

(true ^ false) && !(true | true)

Evaluating the ^ and | expressions gives you:

true && !(true)

This is the same as true and false, which is false; therefore, the variable b in the code above will be the value false.

Now that you have seen the Boolean operators, I am ready to talk about the various control structures in Java, all of which involve some type of Boolean expression.