Sub topic 7
Boolean Values
A boolean is a type that holds one of two values: true or false. Use it for a question with a yes-or-no answer, such as “Does this person have a ticket?”
Boolean is one of Java’s primitive types. A primitive type stores a simple value directly.
Declare and update a boolean
To create a boolean variable, write boolean, a name, and a value:
boolean hasTicket = true;The names true and false must be lowercase. You can change the value later with an assignment, which puts a new value in a variable:
hasTicket = false;These snippets are part of the complete program below.
Make boolean decisions
A comparison checks two values and produces a boolean. For example, age >= 16 is true when age is at least 16. Other comparisons include == for “equal to,” != for “not equal to,” and < for “less than.”
An expression is a piece of code that produces a value. You can combine boolean expressions with these operators:
| Operator | Meaning | Example | Column | Column | Column | Column |
|---|---|---|---|---|---|---|
| `&&` | Both sides must be true | `true && false` is `false` | ||||
| ` | ` | At least one side must be true | `true | false` is `true` | ||
| `!` | Reverses a boolean value | `!true` is `false` |
An if statement uses a boolean condition to choose what to do. It runs one section when the condition is true. An else section can run when the condition is false.
Worked example
This program checks whether a person is old enough and has a ticket. Save it as BooleanExample.java.
class BooleanExample {
public static void main(String[] args) {
int age = 16;
boolean hasTicket = true;
boolean oldEnough = age >= 16;
boolean canEnter = hasTicket && oldEnough;
System.out.println("Can enter: " + canEnter);
hasTicket = false;
canEnter = hasTicket && oldEnough;
if (canEnter) {
System.out.println("Entry allowed");
} else {
System.out.println("Entry denied");
}
System.out.println("Needs ticket: " + !hasTicket);
}
}Compile and run it:
javac BooleanExample.java
java BooleanExampleThe output is:
Can enter: true
Entry denied
Needs ticket: trueAt first, both hasTicket and oldEnough are true, so canEnter is true. The program then changes hasTicket to false and calculates canEnter again. Because one side of && is now false, the result is false.
Changing hasTicket does not change canEnter by itself. The program must assign a new result to canEnter.
Common mistakes
- Use
=to assign a value. Use==to compare values. trueis a boolean value."true"is a string, which is text inside double quotes.- Do not use
1or0in place oftrueorfalsein Java.
Java can also skip part of a boolean expression. With &&, it skips the right side if the left side is false. With ||, it skips the right side if the left side is true. In either case, the left side already determines the result.
Exercise
Given boolean hasKey = true; boolean doorOpen = false; boolean canUnlock = hasKey && !doorOpen;, what is canUnlock? If hasKey later becomes false, does canUnlock change by itself?
Show answer
canUnlock is true. It does not change by itself; you must calculate and assign it again.