TOPIC 2.2 · STUDY
Boolean Expressions
A boolean expression produces true or false. Relational operators compare values, and printing or storing that result keeps true or false—not the numbers or objects that were compared.
2.2.A
Boolean expressions
A boolean expression is any expression whose value is true or false. Relational operators compare two values and yield that boolean result. The comparison does not change the stored values; it answers a yes-or-no question about them.
| Operator | Meaning |
|---|---|
| == | Equal to |
| != | Not equal to |
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
For int and double, these operators compare numeric values. For char, Java compares the characters as numbers in character order, so 'A' is less than 'B'. Equal values make == and <= and >= true; < and > require a strictly smaller or larger value.
int seats = 18;
System.out.print(seats == 18);
The print shows true because 18 equals 18. It does not show the seat count. The variable seats still stores 18 after the comparison runs.
The stored 18 stays in seats. The comparison produces the separate boolean value true.
A boolean variable can store that result. Write boolean ready = seats >= 18; so ready holds true or false. You cannot assign a relational result to an int, and you cannot assign an int directly to a boolean without a comparison.
int span = 24;
boolean wide = span > 20;
System.out.print(wide);
System.out.print(span != 24);
That fragment prints truefalse: wide is true, and span != 24 is false. Arithmetic on the left or right of a relational operator finishes first. With int ticks = 23;, ticks / 5 == 4 is true because int division truncates to 4 before == runs. The same truncated 4 compared with 4.6 is false after promotion. Mixing int and double in division keeps the fractional part, so 11 / 2 == 5 can be true for ints while 11.0 / 2 == 5 is false.
For object references, == and != ask whether both variables refer to the same object, not whether two objects hold matching characters or fields. Two new String("oak") objects with the same text still make == false; an alias that refers to the same object makes == true. You may also write tag == null to test whether a reference refers to no object. The operators <, >, <=, and >= are for primitive numeric comparisons such as int or char, not for String references.
An int can wrap past its largest or smallest value. After that wrap, a later comparison uses the wrapped stored value, so a test such as fuel > 0 may be false even though the arithmetic that overflowed looked like it should stay positive.
Key points
- A boolean expression evaluates to true or false.
- Relational operators are ==, !=, <, >, <=, and >=.
- Printing a comparison prints true or false, not the compared values.
- A boolean variable stores a comparison result; an int cannot.
- int division truncates before == or other relational tests.
- On references, == and != test the same object, not matching content.
- <, >, <=, and >= apply to numeric primitives such as int and char, not to String references.
A comparison prints true or false, not the numbers being compared. System.out.print(seats == 18) shows true or false while seats still holds 18.