# AP® Computer Science A 2.2 Boolean Expressions

Source: https://lockincs.com/ap-csa/unit-2/2-2-boolean-expressions/  
Last reviewed against the College Board Course and Exam Description (CED): 2026-11-12  
CED codes: 2.2.A, 2.2.A.1, 2.2.A.2, 2.2.A.3

A Boolean expression built with relational operators evaluates to a Boolean value, true or false. The operators <, >, <= and >= compare numeric values, while == and != check whether two values are the same. With primitive types, == compares the actual values; with reference types, it compares the object references.

## What you will learn

- 2.2.A Develop code to create Boolean expressions with relational operators and determine the result of these expressions.

## 1\. Relational operators produce true or false

A relational operator compares two values. An expression that uses one evaluates to a Boolean value, `true` or `false` 2.2.A.3. You can print that value or store it in a `boolean` variable.

| Operator | Meaning | Example | Value |
| --- | --- | --- | --- |
| `==` | equal to | `4 == 4` | `true` |
| `!=` | not equal to | `4 != 4` | `false` |
| `<` | less than | `3 < 8` | `true` |
| `>` | greater than | `3 > 8` | `false` |
| `<=` | less than or equal to | `5 <= 5` | `true` |
| `>=` | greater than or equal to | `2 >= 7` | `false` |

```
int age = 16;
boolean canDrive = age >= 16;
boolean canVote = age >= 18;
System.out.println(canDrive);
System.out.println(canVote);
System.out.println(age != 16);
```

Output:

```
true
false
false
```

**Watch out:** `=<`, `=>` and `<>` are not Java operators. The equals sign always comes second.

## 2\. Comparing numbers with <, >, <= and >=

The operators `<`, `>`, `<=` and `>=` compare numeric values to find their relationship 2.2.A.2. They work with `int` and `double` values, and you can mix the two types: Java widens the `int` to a `double` before comparing.

The arithmetic on each side happens first, with the normal Java rules. That makes integer division a classic trap:

```
int goals = 7;
int games = 2;
System.out.println(goals / games > 3);
System.out.println(goals / games >= 3);
System.out.println((double) goals / games > 3);
```

Output:

```
false
true
true
```

`goals / games` is `7 / 2`, which is `3` in integer division, and `3 > 3` is false. Casting one operand to `double` gives `3.5`, and `3.5 > 3` is true.

**Watch out:** a range such as “between 0 and 10” cannot be written as `0 < x < 10` in Java. The first comparison gives a `boolean`, and a `boolean` cannot be compared with a number.

```
int x = 5;
boolean inRange = 0 < x < 10;
```

```
error: bad operand types for binary operator '<'
```

You will join two comparisons with `&&` in Topic 2.5.

## 3\. == and != with primitive values

The operators `==` and `!=` check whether two values are the same 2.2.A.1. With primitive types (`int`, `double`, `boolean`), they compare the actual primitive values.

```
int lives = 3;
int startLives = 3;
boolean gameOver = false;
System.out.println(lives == startLives);
System.out.println(lives != 0);
System.out.println(gameOver == false);
```

Output:

```
true
true
true
```

**Watch out (= versus ==):** one equals sign is assignment, two are comparison. Using `=` where Java expects a Boolean expression is a compile error for `int` values:

```
int lives = 3;
if (lives = 0) {
    System.out.println("Game over");
}
```

```
error: incompatible types: int cannot be converted to boolean
```

**Watch out (double values):** round-off error can make two values that should be equal compare as different.

```
double total = 0.1 + 0.2;
System.out.println(total == 0.3);
System.out.println(total);
```

Output:

```
false
0.30000000000000004
```

## 4\. == and != with reference types

With reference types, `==` and `!=` compare the object references, not the contents of the objects 2.2.A.1. A reference tells Java where an object is. Two references are `==` only when they refer to the same object.

```
String a = new String("goal");
String b = new String("goal");
String c = a;
System.out.println(a == b);
System.out.println(a == c);
System.out.println(a.equals(b));
```

Output:

```
false
true
true
```

-   `a == b` is false: `new` created two separate objects, even though both hold the letters “goal”.
-   `a == c` is true: `c` holds a copy of the reference in `a`, so both refer to the same object.
-   `a.equals(b)` is true: `equals` compares the characters.

**Watch out:** this is the most common String trap on the AP® exam. To ask “do these strings have the same characters?”, use `equals`, never `==`. Topic 2.6 goes deeper into comparing object references.

## 5\. Store the result and reuse it

Because a relational expression evaluates to a Boolean value, you can store the result in a `boolean` variable with a clear name. This makes later code easier to read.

```
double temperature = 38.4;
boolean hasFever = temperature >= 38.0;
System.out.println("Fever: " + hasFever);
```

Output:

```
Fever: true
```

Read `hasFever = temperature >= 38.0` from right to left: first the comparison gives `true`, then `=` stores `true` in `hasFever`.

## Common mix-ups

- Not true: = and == do the same thing. = is assignment: it stores a value. == is a relational operator: it compares two values and gives true or false. if (x = 5) does not compile in Java.
- Not true: == checks whether two strings have the same letters. With reference types, == compares the object references, so it asks whether both variables refer to the very same object. Use equals to compare the characters.
- Not true: 7 / 2 > 3 is true because 7 divided by 2 is 3.5. 7 / 2 is integer division, so it gives 3, and 3 > 3 is false. The arithmetic happens before the comparison, with the usual Java rules.

## In 30 seconds

- Relational operators: == != < > <= >=. Every relational expression evaluates to true or false.
- <, >, <= and >= compare numeric values.
- == and != with primitive values compare the values themselves.
- == and != with references compare the references (same object or not), not the contents.
- Arithmetic runs before the comparison, so integer division and round-off error affect the result.

## Checkpoint: 5 practice questions

### Question 1

Consider the following code segment. What is printed as a result of executing the code segment?

```java
int a = 17;
int b = 5;
System.out.println((a / b >= 3.4) + " " + (a % b == 2));
```

- A. false true
- B. true true
- C. false false
- D. true false

**Answer: A.** `a / b` is integer division, so it is 3, and `3 >= 3.4` is false. `a % b` is the remainder 2, so `a % b == 2` is true. Choosing true for the first comparison treats `17 / 5` as 3.4, which only happens with a double operand.

### Question 2

Consider the following code segment. What is printed as a result of executing the code segment?

```java
String s1 = new String("go");
String s2 = new String("go");
String s3 = s1;
System.out.println((s1 == s2) + " " + (s1 == s3) + " " + s1.equals(s2));
```

- A. true true true
- B. false false true
- C. true false false
- D. false true true

**Answer: D.** With reference types, `==` compares object references. `s1` and `s2` refer to two separate objects, so `s1 == s2` is false; `s3` holds the same reference as `s1`, so `s1 == s3` is true. `equals` compares the characters, so `s1.equals(s2)` is true. "true true true" wrongly assumes `==` compares the characters.

### Question 3

The `int` variable `score` has been declared and initialized. Which of the following statements compiles and stores `true` in `top` exactly when `score` is 90 or more?

- A. `boolean top = score => 90;`
- B. `boolean top = score >= 90;`
- C. `boolean top = (score = 90);`
- D. `boolean top = score > 90;`

**Answer: B.** `>=` is the relational operator for "greater than or equal to", and the comparison evaluates to a Boolean value that can be stored. `=>` is not a Java operator, and `score = 90` is an assignment that gives an `int`, so neither compiles. `score > 90` compiles but is false when `score` is exactly 90.

### Question 4

Consider the following code segment. What is printed as a result of executing the code segment?

```java
double price = 0.1 * 3;
System.out.println(price == 0.3);
```

- A. true
- B. 0.3
- C. Nothing is printed because the code does not compile.
- D. false

**Answer: D.** `==` compares the two primitive double values exactly. Because of round-off error, `0.1 * 3` is stored as 0.30000000000000004, which is not equal to 0.3, so the expression is false. The code compiles, and `println` prints the Boolean value, not 0.3.

### Question 5

Consider the following code segment. Which of the following best describes the result of compiling and running it?

```java
int lives = 2;
if (lives = 0) {
    System.out.println("Game over");
}
```

- A. Nothing is printed.
- B. The code does not compile.
- C. `Game over` is printed.
- D. An exception is thrown when the code runs.

**Answer: B.** `lives = 0` is an assignment, and its value is the `int` 0. An `if` condition must be a Boolean expression, so the compiler reports that an `int` cannot be converted to `boolean`. Nothing runs, so the program cannot print or throw anything. The intended comparison is `lives == 0`.

## Questions students ask

**What is a Boolean expression in Java?**

An expression that evaluates to a Boolean value, true or false. Expressions built with relational operators such as x > 10 or name != null are Boolean expressions.

**What are the relational operators in AP® Computer Science A?**

== (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to) and >= (greater than or equal to).

**Why does == not work for comparing strings?**

String is a reference type, so == compares the references, not the characters. Two different String objects with the same characters are not ==. Use the equals method to compare the characters.

**Can I write 0 < x < 10 in Java?**

No. 0 < x gives a boolean, and Java cannot compare a boolean with 10, so the code does not compile. Write two comparisons joined with && (Topic 2.5).

---

© LockInCS, licensed CC BY-NC-ND 4.0. AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this website.
