Reference Guide

Key Ideas

  • A Boolean expression evaluates to exactly one of two values: true or false.
  • Relational operators compare two values: == != < > <= >=.
  • == asks “are these equal?” while = assigns a value. Mixing them up is the most common bug in this unit.
  • Arithmetic runs first, then the comparison: x + 1 > 5 adds before it compares.
  • Relational operators work on numbers (int, double) and char. Use .equals for Strings (more in 2.6).
  • A Boolean value can be stored in a boolean variable: boolean isAdult = age >= 18;

The Six Relational Operators

Operator Question it asks Example (x = 7) Value
== equal to? x == 7 true
!= not equal to? x != 7 false
< less than? x < 10 true
> greater than? x > 10 false
<= less than or equal? x <= 7 true
>= greater than or equal? x >= 8 false
  • Integer division still applies: 10 / 4 == 2 is true because 10 / 4 is 2.
  • % gives the remainder: x % 2 == 0 is the standard test for “is x even?”.

College Board Requirements

AP CSA Unit 2, Topic 2.2: Boolean Expressions.

This lesson is built directly against the College Board’s stated learning objective for this topic:

Evaluate Boolean expressions that use relational operators in program code.

Topic 2.2, Boolean Expressions, is required content on the AP Computer Science A Exam. The learning objective quoted above is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.


Lesson Plan

Learning Objective: Evaluate Boolean expressions that use relational operators in program code.

Success Criteria: You can predict whether a relational expression is true or false, store the result in a boolean variable, and explain why = and == are not interchangeable.

Tech Talk & Introduction

A Boolean expression is a question with only two possible answers. Java answers it with true or false, and that value can be stored just like a number.

  • The relational operators are ==, !=, <, >, <= and >=.
  • = assigns a value. == asks whether two values are equal. Mixing them up is the most common bug in this topic.
  • The result of a comparison is a boolean, so boolean passed = score >= 60; is legal and often clearer than repeating the comparison.

Code Examples

A. Simple: Booleans in Code

Run this program. Then change temperature to 100, 72, and -5 and predict each line before you press Run.

Code Runner Challenge

Boolean demo: store comparisons in boolean variables and print them.

View IPYNB Source
// CODE_RUNNER: Boolean demo: store comparisons in boolean variables and print them.
public class BooleanDemo {
    public static void main(String[] args) {
        int temperature = 85;
        int limit = 90;

        boolean isHot = temperature > limit;
        boolean isFreezing = temperature <= 32;
        boolean isEven = temperature % 2 == 0;

        System.out.println("temperature > limit: " + isHot);
        System.out.println("temperature <= 32: " + isFreezing);
        System.out.println("temperature % 2 == 0: " + isEven);
        System.out.println("temperature != limit: " + (temperature != limit));
        System.out.println("temperature + 5 >= limit: " + (temperature + 5 >= limit));
    }
}
BooleanDemo.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Hacks & Practice Tasks

Activity 1: Live Comparison Lab

Move the sliders. Every println line re-evaluates instantly. Try to make every line print true, then every line print false.

Comparison Lab
Change x and y and watch each Boolean expression update.

Activity 2: True or False?

Given the three variables below, sort every expression into the TRUE or FALSE bucket. Watch out for integer division.

int a = 10;
int b = 4;
double c = 2.5;
Sort the Expressions
Ten expressions. Evaluate each one in your head first.

Click an item, then click a bucket. Or drag it. Click an item inside a bucket to send it back.

TRUE
FALSE

Activity 3: Spot the Bug

Bug Hunt
Three quick questions. Each one is a classic AP exam trap.
Question 1

Which line will NOT compile?

Question 2

What is the value of 7 / 2 == 3.5?

Question 3

Which expression is true exactly when n is a multiple of 5?

Popcorn Hack

This program has two bugs. One stops it from compiling, the other prints the wrong answer. Fix both so it prints Bonus unlocked: true and Exact score: false.

Code Runner Challenge

Popcorn hack: fix the two bugs so both lines print the expected values.

View IPYNB Source
// CODE_RUNNER: Popcorn hack: fix the two bugs so both lines print the expected values.
public class BonusCheck {
    public static void main(String[] args) {
        int score = 120;
        int target = 100;

        // Bug 1: this line does not compile
        boolean bonus = score => target;

        // Bug 2: this should ask whether score is EXACTLY equal to target
        boolean exact = score != target;

        System.out.println("Bonus unlocked: " + bonus);
        System.out.println("Exact score: " + exact);
    }
}
BonusCheck.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Homework Hack

Short and specific.

  1. Complete the runner below: write the four Boolean expressions described in the comments and print them.
  2. Answer the two quick checks under it.

Code Runner Challenge

Homework: replace each false with a Boolean expression so the expected values print.

View IPYNB Source
// CODE_RUNNER: Homework: replace each false with a Boolean expression so the expected values print.
public class SpeedCheck {
    public static void main(String[] args) {
        int speed = 68;
        int limit = 65;
        int age = 17;

        // 1. true when the driver is over the limit
        boolean speeding = false; // TODO

        // 2. true when the driver is going exactly the limit
        boolean exactLimit = false; // TODO

        // 3. true when the driver is at least 18
        boolean adult = false; // TODO

        // 4. true when speed is an odd number (use %)
        boolean oddSpeed = false; // TODO

        System.out.println("speeding: " + speeding);      // expected: true
        System.out.println("exactLimit: " + exactLimit);  // expected: false
        System.out.println("adult: " + adult);            // expected: false
        System.out.println("oddSpeed: " + oddSpeed);      // expected: false
    }
}
SpeedCheck.main(null);
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...
Homework Quick Checks
Type the value Java produces. Answers are true or false.
Question 1

What does this print?

int n = 15;
System.out.println(n % 5 == 0);
Question 2

What does this print?

int a = 9;
int b = 2;
System.out.println(a / b == 4.5);

References

College Board Course and Exam Description

Topic 2.2, Boolean Expressions, is required content on the AP Computer Science A Exam. The learning objective quoted in section 3 (College Board Requirements) is the College Board’s own wording for this topic, and the activities, Popcorn Hack and Homework Hack are all written against it.

Outside Academic Reference

The relational operators and the boolean type behave exactly as the language specification defines them: a numeric comparison produces a value of type boolean, which is why it can be assigned to a variable.

Reference List

College Board. (2025). AP Computer Science A: Course and exam description. https://apcentral.collegeboard.org/courses/ap-computer-science-a

Gosling, J., Joy, B., Steele, G., Bracha, G., Buckley, A., Smith, D., & Bierman, G. (2023). The Java language specification: Java SE 21 edition. Oracle America. https://docs.oracle.com/javase/specs/jls/se21/html/

All Java lessons