Sub topic 9
Combining Boolean Expressions
A Boolean value is either true or false. A Boolean expression is an expression that produces one of those values. For example, age >= 18 is true when age is at least 18.
You can combine Boolean expressions to ask a more detailed question. For example, can a person enter if they have a ticket or are a guest, and they are not banned?
An operator is a symbol that performs an operation. An operand is a value or expression the operator works on. Java has three commonly used Boolean operators:
&&means AND.||means OR.!means NOT.
These operators help turn several simple checks into one useful result.
AND: Both Conditions Must Be True
The && operator combines two Boolean operands. Its result is true only when both operands are true.
For example, hasTicket && hasId is true only if the person has both a ticket and an ID.
| First operand | Second operand | Result of `&&` |
|---|---|---|
| `false` | `false` | `false` |
| `false` | `true` | `false` |
| `true` | `false` | `false` |
| `true` | `true` | `true` |
You can also combine comparisons. The expression age >= 18 && age < 65 is true when age is at least 18 and less than 65. Each comparison produces a Boolean value, and && combines those values.
OR: At Least One Condition Must Be True
The || operator produces true when either operand is true, including when both are true. It produces false only when both operands are false.
For example, hasTicket || isGuest is true if the person has a ticket, is a guest, or both.
| First operand | Second operand | Result of ` | Column | ` |
|---|---|---|---|---|
| `false` | `false` | `false` | ||
| `false` | `true` | `true` | ||
| `true` | `false` | `true` | ||
| `true` | `true` | `true` |
NOT: Reverse a Boolean Value
The ! operator works on one Boolean operand. It changes true to false and false to true.
| Operand | Result of `!` |
|---|---|
| `false` | `true` |
| `true` | `false` |
For example, !isBanned is true when isBanned is false. Read it as “not banned.”
You can put parentheses around an expression to reverse its whole result. For example, !(hasTicket || isGuest) means “the person has neither a ticket nor guest status.” First, find the result inside the parentheses. Then reverse it.
Worked Example: Entry Rules
Suppose an event allows entry when a person has a ticket or is a guest. A banned person cannot enter, even if they meet one of those conditions.
Save this program as BooleanPractice.java:
public class BooleanPractice {
public static void main(String[] args) {
boolean hasTicket = false;
boolean isGuest = true;
boolean isBanned = false;
boolean hasBoth = hasTicket && isGuest;
boolean hasEither = hasTicket || isGuest;
boolean notBanned = !isBanned;
boolean canEnter = (hasTicket || isGuest) && !isBanned;
System.out.println("Has both: " + hasBoth);
System.out.println("Has either: " + hasEither);
System.out.println("Not banned: " + notBanned);
System.out.println("Can enter: " + canEnter);
}
}Compile and run it:
javac BooleanPractice.java
java BooleanPracticeThe output is:
Has both: false
Has either: true
Not banned: true
Can enter: trueTo work out canEnter, follow the rule in parts:
hasTicket || isGuestistruebecauseisGuestistrue.!isBannedistruebecauseisBannedisfalse.- The two results are combined as
true && true, which istrue.
Try changing isBanned to true and running the program again. The person is still a guest, but canEnter becomes false.
Grouping Boolean Expressions
When an expression contains several operators, Java uses precedence rules to decide how to group them. Among these three Boolean operators, ! groups first, then &&, then ||. Parentheses can make the grouping clear or change it.
For example, true || false && false groups as true || (false && false). The result is true.
But (true || false) && false has a different grouping. The first part is true, so the result is true && false, or false.
Parentheses also control what ! reverses. Using the values from the program:
!(hasTicket || isGuest)isfalse. The expression inside istrue, and!reverses it.!hasTicket || isGuestistrue. Here,!reverses onlyhasTicket, making the left operandtrue.
Use parentheses when they help a reader see your intended rule, even when Java’s default grouping would give the same result.
Common Mistakes
Combining the Wrong Things
Both operands of && and || must be Boolean values or expressions. For example, age >= 18 && age < 65 has a comparison on each side.
Do not write age >= 18 && < 65. The second comparison needs its own left operand: age.
A number is not a Boolean value in Java. For example, age && hasId is not valid if age is an int.
Using OR When You Need AND
For age >= 18 && age < 65, both age limits must be met. Replacing && with || changes the meaning: it asks whether either limit is met. An age of 10 would pass the second check, even though it is below 18.
Read each combined expression aloud and test it with values near the limits.
Comparing a Boolean to `true`
If hasTicket is already a Boolean variable, use hasTicket rather than hasTicket == true. Both give the same result, but the shorter form is easier to read. Likewise, use !hasTicket to mean that the person does not have a ticket.
Assuming Both Sides Are Always Checked
Java may skip the right operand of && or || when the left operand already determines the result. This is called short-circuit evaluation. For example, if the left side of && is false, the result must be false, so Java does not need to check the right side.
Exercise
If hasTicket is false and isGuest is true, what are the results of hasTicket && isGuest and hasTicket || isGuest?
Show answer
The AND result is false. The OR result is true.
Exercise
What is the result of !(true || false), and why?
Show answer
It is false. The expression in parentheses is true, and ! reverses it.
Exercise
A person can enter when they have a ticket and are not banned. Write a Boolean expression using hasTicket and isBanned.
Show answer
hasTicket && !isBanned
Exercise
What is the result of true || false && false? What result do you get if you group it as (true || false) && false?
Show answer
The first result is true because && groups before ||. The second result is false.