3.4 Boolean Operators

Just as arithmetic expressions can be acted upon by arithmetic operators to produce new arithmetic expressions, boolean expressions can be acted upon by boolean operators to produce new boolean expressions. There are three commonly used boolean operators in Java: ! (not), && (and), and || (or). Appendix G discusses other, less commonly used, boolean operators. If p and q are both of type boolean, then

1. !p (not p) has the value true if and only if p has the value false.

2. p && q (p and q) has the value true if and only if both p and q have the value true.

3. p || q (p or q) has the value true if and only if at least one of p or q has the value true. The results of applying the boolean operators can be summarized in truth tables as shown below.

Notice that the truth table for p || q has the value true not only if exactly one of them is true but also if both p and q are true. This may seem strange at first, but we often use the word "or" in this sense. For example, if one is asked, "do you take cream or sugar with your coffee?" it is reasonable to reply, "yes, I take both." Taking "cream or sugar" includes the possibility of taking both.

If an expression contains more than one boolean operator, then, as with arithmetic expressions, precedence rules determine the order in which operations are performed. If we do not use parentheses, then the precedence order of the three boolean operations is
As with arithmetic operations, the precedence rules can be overridden by using parentheses.

These operators can be mixed in expressions with other operators. The precedence order for the operators that we have seen up to now is

Operations with equal precedence are performed from left to right with the exception of assignment operations which (as we have already seen) are performed from right to left.

In evaluating expressions involving && or ||, Java uses a technique known as lazy evaluation or short circuit evaluation or conditional evaluation. To show how this works, consider the expression p && q. Evaluating this from left to right, if p is false, then the entire expression must be false, no matter what value q has. Since the value of the expression is known without evaluating q, Java does not even look at q. Of course, if p is true,then Java must examine q to determine the value of the entire expression.

A similar situation arises with expressions of the form p || q. Here, if p is true, then the entire expression must be true so Java can again be lazy and not bother looking at q.

Boolean expressions can often be simplified using a pair of rules known as de Morgan's laws, in honour of the logician Augustus de Morgan. To illustrate one of these laws, consider the statement "This book is boring and this course is useless." For this statement to be true, both parts of it must be true. For the statement to be false, one or both of its parts must be false. Thus, the negation of this statement can be written in the form "This book is not boring or this course is not useless." If we let b represent "This book is boring" and we let u represent "This course is useless", then, from the above argument, the statement "it is not true that this book is boring and this course is useless" could be written as ! (b && u). On the other hand, the statement "this book is not boring or this course is not useless" could be written as !b || !u. Since these statements are equivalent, then

This is one of de Morgan's laws. The other is formed by interchanging the positions of && and ||. As an example of the second form, the statement "I do not like green eggs or ham" means "I don't like green eggs and I don't like ham." Using the symbol to mean that two boolean expressions are equivalent, we can write the two laws as follows:




Exercise 3.4

  1. Evaluate each expression assuming that the following declarations have been made.
  2. Simplify as much as possible.


  3. Write a boolean expression that will be true if and only if the int variable i satisfies the condition 0 i 5.

  4. Use de Morgan's laws and the fact that !!p is equivalent to p to simplify each expression.


  5. In addition to de Morgan's laws, there are a number of other laws that can be used to simplify boolean expressions. A useful list of equivalences is given in the following table. (The symbol is used to mean that two boolean expressions are equivalent.)



    Use these relationships to simplify each of the following expressions as much as possible.



  6. The exclusive or operator, which is sometimes written as ,has the following truth table.



    Write a Java expression that is true if and only if p q is true.