AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/2/2-2)
Unit 2 · Topic 2.2
2.2 Boolean Expressions
A Boolean expression is a question your code asks that has a true or false answer. This topic covers the six relational operators that build those questions, and the important difference between comparing primitive values and comparing object references.
Key terms
- Boolean expression
- relational operator
==vs!=boolean
The relational operators
A relational operator compares two values and produces a boolean. Every if statement and loop you write depends on one of these.
| Operator | Means | Example that is true |
|---|---|---|
| == | is equal to | 4 == 4 |
| != | is not equal to | 4 != 5 |
| < | is less than | 3 < 8 |
| > | is greater than | 8 > 3 |
| <= | is less than or equal to | 5 <= 5 |
| >= | is greater than or equal to | 9 >= 2 |
How they're evaluated
Arithmetic happens before comparing. In a + 3 >= b, Java first works out a + 3, then compares the result with b. You can add parentheses to make this clearer, but you don't need them.
You can compare an int with a double. Java widens the int to a double first, so 7 == 7.0 is true.
The result of a comparison is a boolean value, so you can store it in a boolean variable, print it, or return it from a method. Writing return score >= 70; is shorter than an if statement that returns true or false.
Two common slips: = is assignment, not comparison, so always use == in a condition. And you can't chain comparisons like you do in math. 1 < x < 10 doesn't compile, because 1 < x is a boolean and you can't ask whether a boolean is less than 10. You'll learn the right way, x > 1 && x < 10, in 2.5.
Primitives versus references
With primitive types (int, double, boolean), == and != compare the actual values.
With reference types, a variable holds a reference to an object (1.12). So == asks a different question: do these two variables refer to the very same object? Two separate objects can hold the same data and still make == false.
String s1 = new String("go");
String s2 = new String("go");
String s3 = s1;
System.out.println((s1 == s2) + " " + (s1 == s3));
s1 and s2 were each made with new, so they're different objects with the same letters, and s1 == s2 is false. s3 was assigned s1, so both refer to one object, and s1 == s3 is true. To compare the contents of strings, use equals (1.15 and 2.6).
The operators <, >, <= and >= only work on numbers. To put strings in order, use compareTo.
Worked examples
Try each one yourself first, then open the solution.
- Example 1
Evaluating comparisons
What does this code print?
int a = 7; int b = 10; double c = 7.0; boolean r1 = a < b; boolean r2 = a == c; boolean r3 = b % 2 != 0; boolean r4 = a + 3 >= b; System.out.println(r1 + " " + r2 + " " + r3 + " " + r4);Show the solutionHide the solution
- Step 1:
r1: is 7 less than 10? Yes,true. - Step 2:
r2:ais anintandcis adouble. The 7 is widened to 7.0, and 7.0 equals 7.0, sotrue. - Step 3:
r3:b % 2is10 % 2, which is 0. Is 0 not equal to 0? No, sofalse. (This expression tests whetherbis odd.) - Step 4:
r4: arithmetic first,7 + 3is 10. Is 10 greater than or equal to 10? Yes,true.
Answer: It prints
true true false true. - Step 1:
- Example 2
Spotting the error
A student wants to check whether
xis between 1 and 10, so they write:int x = 5; boolean ok = 1 < x < 10;What happens?Show the solutionHide the solution
- Step 1: Java evaluates
1 < xfirst, which gives aboolean(here,true). - Step 2: The next step would be
true < 10, but<can't compare abooleanwith a number. - Step 3: So the compiler rejects the line. This is a syntax error, caught before the program runs.
Answer: It doesn't compile. Write
x > 1 && x < 10instead (see 2.5). - Step 1: Java evaluates
Common mistakes
- Using
=instead of==in a condition.=stores a value;==compares. - Comparing strings or other objects with
==when you mean "same contents". Useequals. - Writing
1 < x < 10like in math. Java needs two comparisons joined with&&.
On the exam
- Many multiple-choice questions turn on one comparison:
<versus<=decides whether the boundary value counts. Test the exact boundary value in your head. - In free-response answers, returning a comparison directly (
return count > 0;) is correct and saves time.
Connected topics
Videos
Check yourself
3 questions on 2.2 Boolean Expressions. Pick an answer to see if you got it, and why.
Consider the following code segment.int a = 7;
int b = 3;
boolean p = a % b == 1;
boolean q = a / b >= 2.5;
System.out.println(p + " " + q);What is printed as a result of executing the code segment?
Assume n is an int variable with a positive value. Which of the following expressions evaluates to true exactly when n is a multiple of 3?
Consider the following class.public class Counter
{
private int value;
public Counter(int v)
{
value = v;
}
public int getValue()
{
return value;
}
}What is printed as a result of executing the following code segment?Counter a = new Counter(5);
Counter b = new Counter(5);
Counter c = a;
System.out.println((a == b) + " " + (a == c));
0 of 3 answered