Video lecture
Watch on YouTube(opens in a new tab)Sub topic 7
Boolean Values
A boolean is a value that is either true or false. In Java, the primitive type for these values is named boolean. A primitive type is a built-in type for a simple value.
Booleans are useful when a program needs a yes-or-no answer:
- Is a door open?
- Does a person have a ticket?
- Is a number greater than another number?
- Should the program show a message?
The words true and false are boolean literals. A literal is a value written directly in code. Both words must be lowercase: True and FALSE are not boolean literals in Java.
Storing a Boolean Value
A variable is a named place for a value. You can declare a boolean variable and give it a value like this:
boolean hasTicket = true;
The type is boolean, the variable name is hasTicket, and the starting value is true. The = sign assigns the value to the variable.
A good boolean name often reads like a yes-or-no question. Names such as isOpen, hasTicket, and canEnter make it easier to understand what true means.
You can change the value later:
hasTicket = false;
A boolean can hold only true or false. You cannot assign 1 or 0 to a boolean variable. Java does not treat numbers as boolean values.
Using a Boolean to Make a Choice
A condition is a true-or-false test. An if statement checks a condition and runs some code if the condition is true. An else part can run different code when the condition is false.
The following program checks whether a person can enter. It also shows what happens when a value used in a check changes.
Save the program in a file named BooleanValues.java:
class BooleanValues {
public static void main(String[] args) {
boolean hasTicket = true;
boolean gateOpen = false;
boolean canEnter = hasTicket && gateOpen;
System.out.println("Can enter at first: " + canEnter);
gateOpen = true;
System.out.println("Old result: " + canEnter);
canEnter = hasTicket && gateOpen;
System.out.println("Result after checking again: " + canEnter);
if (canEnter) {
System.out.println("Welcome!");
} else {
System.out.println("Please wait.");
}
}
}The symbol && means “and.” Both hasTicket and gateOpen must be true for canEnter to be true.
Run the program with these commands:
javac BooleanValues.java
java BooleanValuesThe output is:
Can enter at first: false
Old result: false
Result after checking again: true
Welcome!At first, the gate is closed, so canEnter is false. Opening the gate does not change the value already stored in canEnter. The program must perform the check again and assign the new result.
After that check, canEnter is true, so the if part prints Welcome!. If canEnter were false, the else part would print Please wait. instead.
Comparing Values
A boolean expression is code that produces true or false. Comparing two values is one way to make a boolean expression.
An operator is a symbol that performs an action, such as checking whether two values are equal. These comparison operators produce boolean results:
| Operator | Meaning | Example | Result |
|---|---|---|---|
| `==` | Equal to | `5 == 5` | `true` |
| `!=` | Not equal to | `5 != 5` | `false` |
| `>` | Greater than | `5 > 3` | `true` |
| `>=` | Greater than or equal to | `5 >= 5` | `true` |
| `<` | Less than | `5 < 3` | `false` |
| `<=` | Less than or equal to | `5 <= 3` | `false` |
You can store a comparison result in a boolean variable. For example, boolean isAdult = 20 >= 18; gives isAdult the value true.
Be careful with = and ==. The first assigns a value. The second compares values.
Combining and Reversing Booleans
Java has operators for combining boolean values. These are called logical operators.
| Operator | Meaning | Result | Column | Column |
|---|---|---|---|---|
| `&&` | And | True only when both values are true | ||
| ` | ` | Or | True when at least one value is true | |
| `!` | Not | Reverses a boolean value |
For example, hasTicket && gateOpen is true only if the person has a ticket and the gate is open. The expression !gateOpen is true when gateOpen is false.
This table shows every combination for two boolean values:
| First value | Second value | First `&&` second | First ` | Column | ` second |
|---|---|---|---|---|---|
| `false` | `false` | `false` | `false` | ||
| `false` | `true` | `false` | `true` | ||
| `true` | `false` | `false` | `true` | ||
| `true` | `true` | `true` | `true` |
Notice that “or” includes the case where both values are true. It does not mean that exactly one must be true.
Use parentheses to make a longer check easier to read. For example, (hasTicket || hasPass) && gateOpen asks whether the person has a ticket or a pass, and whether the gate is open.
Checks That Can Stop Early
Java checks the left side of && and || first. Sometimes it does not need to check the right side:
- With
&&, if the left side is false, the whole expression is false. Java skips the right side. - With
||, if the left side is true, the whole expression is true. Java skips the right side.
This is called short-circuiting. It matters when the right side does more than read a variable: that work may not happen.
Common Mistakes
A boolean value is not the same as the text "true". The value true is a boolean. The quoted text "true" is a string, which is text in Java.
Also, a variable holds the result of a check, not a promise to repeat that check. In the worked program, changing gateOpen did not change canEnter until the program assigned a fresh result.
Finally, you usually do not need to compare a boolean with true or false:
- Use
if (ready)to check whetherreadyis true. - Use
if (!ready)to check whetherreadyis false.
Practice
Exercise
A person has a pass, and the park is not closed. What value does boolean mayVisit = hasPass && !isClosed; produce if hasPass is true and isClosed is false?
Show answer
It produces true. The pass check is true, and !isClosed is also true because it reverses false.
Exercise
After the first exercise, isClosed changes to true. Does mayVisit change by itself? What happens if you assign the result of hasPass && !isClosed to mayVisit again?
Show answer
mayVisit stays true until it is assigned a new value. The new check gives it false, because !isClosed is now false.