Skip to main content

2.2 Boolean Expressions

add to ChatGPT
Verified for the 2027 examUpdated October 2026Fiveable Content Team
Fiveable

💻AP Computer Science A Unit 2 Review

QR code for AP Computer Science A practice questions

2.2 Boolean Expressions

TLDR

A Boolean expression in AP Computer Science A is any comparison that evaluates to true or false. In this topic you build expressions with relational operators like ==, !=, <, >, <=, and >=, and you learn the key rule that primitive comparisons check actual values while reference comparisons check whether two variables point to the same object.

What Is a Boolean Expression in AP CSA?

A Boolean expression is any Java expression that evaluates to either true or false. In AP CSA Topic 2.2, most Boolean expressions come from relational operators, such as ==, !=, <, >, <=, and >=, and they become the conditions used by if statements and loops.

A Boolean expression here is a relational comparison like == or < that evaluates to true or false, and a wrong comparison can make a method loop forever or branch the wrong way. Logical operators (&&, ||, !), short-circuit evaluation, and equivalent expressions come in later topics, so focus on building and evaluating comparisons.

What Relational Operators Compare for Primitives and Objects

  • A relational expression always evaluates to a Boolean value (true or false).
  • == and != test whether two values are the same. For primitives, this compares the actual values. For reference types, this compares whether the references point to the same object.
  • <, >, <=, and >= compare numeric values to show how they relate in size.
  • Use == for comparing primitive values, but use a method like .equals when you want to compare the contents of objects.
  • Comparing object references with == only tells you if they are the exact same object, not if they hold equal data.

Boolean Expressions: The Basics

A Boolean expression is any expression that evaluates to true or false. Every comparison you write is essentially a yes/no question your program can answer, like "Is this score at least 70?" or "Are these two values equal?"

Relational operators are the tools you use to build these comparisons. The result is always a Boolean value, which is why these expressions work as the conditions inside if statements and loops.

Relational Operators

Equality Operators: == and !=

The == operator checks if two values are the same, and != checks if they are different. What they actually compare depends on the type:

  • With primitive types (int, double, char, boolean), == compares the actual stored values.
  • With reference types (objects), == compares the object references, meaning it checks whether both variables point to the exact same object in memory.
</>Java
int a = 5;
int b = 5;
boolean sameValue = (a == b);   // true, primitive values match

int x = 10;
boolean different = (x != 7);   // true, 10 is not 7

Ordering Operators: <, >, <=, >=

These operators compare numeric values to determine their relationship in size:

</>Java
int score = 85;
boolean passing = score >= 70;    // true
boolean perfect = score == 100;   // false
boolean isMinor = score < 18;     // false (just comparing numbers here)

Each of these expressions produces a Boolean value you can store in a variable or drop directly into a condition.

Comparing Objects vs Primitives

Primitive variablesString object references
p: [4]    q: [4]s1 → [String object 1: "hello"]    s2 → [String object 2: "hello"]
p == qs1 == s2
Result: trueResult: false
s1.equals(s2)
Result: true

This is the part that trips up the most students, so slow down here.

When you compare primitives with ==, you are comparing their actual values:

</>Java
int p = 4;
int q = 4;
System.out.println(p == q);   // true

When you compare objects with ==, you are checking whether the two variables refer to the same object, not whether they hold equal data:

</>Java
String s1 = new String("hello");
String s2 = new String("hello");
System.out.println(s1 == s2);        // false, two different objects
System.out.println(s1.equals(s2));   // true, same contents

Even though s1 and s2 contain the same characters, == returns false because they are separate objects in memory. To compare what objects actually contain, classes often define an .equals method that checks their attributes. Use .equals when you care about the contents, and == when you care about whether it is literally the same object.

You can also compare a reference with null using == or != to check whether a variable actually points to an object:

</>Java
String name = null;
boolean hasName = (name != null);   // false

Tracing Comparisons and Writing Correct Conditions

Code Tracing: When a multiple-choice question asks for the result of a comparison, identify the types involved first. If you see == with objects, the answer hinges on whether the two references point to the same object, not on whether their contents look the same. A common trap answer assumes == compares object contents the way .equals does.

Free Response: When you write a method, the conditions inside your if statements and loops are Boolean expressions. Pick the right operator for the comparison: == and != for checking sameness, and <, >, <=, >= for size relationships. For comparing the contents of two objects such as Strings, reach for .equals instead of ==.

Common Trap: Mixing up = and == is a frequent error. A single = assigns a value, while == compares two values. In Java, using = with an int or double in a condition causes a compile error, but with a boolean variable (e.g., if (flag = true)) it compiles and assigns the value, giving a wrong result.

Common Misconceptions

  • Misconception: == compares object contents.
    Actually: It does not. For objects, == only checks whether two references point to the same object. Use .equals to compare contents.
  • Misconception: == and = are interchangeable.
    Actually: They are not. = assigns a value; == compares values. Using = where you need a comparison is a mistake.
  • Misconception: Relational expressions return numbers.
    Actually: A comparison always evaluates to a Boolean value (true or false), not a number.
  • Misconception: .equals and == always give the same answer for Strings.
    Actually: They can differ. Two String objects with identical characters can return false with == but true with .equals.
  • Misconception: You need logical operators like && and || to make a Boolean expression.
    Actually: A single relational comparison is already a complete Boolean expression. Logical operators and short-circuit evaluation come in later topics.

Practice Questions for 2.2

1 of 10

Consider the following code segment.

</>Java
String s1 = new String("apple");
String s2 = new String("apple");
String s3 = s1;

boolean b1 = s1 == s2;
boolean b2 = s1.equals(s2);
boolean b3 = s1 == s3;

System.out.print(b1 + ", " + b2 + ", " + b3);

What is printed as a result of executing this code segment?

A.

false, false, true

B.

false, true, true

C.

true, true, true

D.

true, true, false

Vocabulary

The following words are mentioned explicitly in the AP® course framework for this topic.

Term

Definition

Boolean expression

An expression that evaluates to either true or false, used to control the execution of loops and conditional statements.

Boolean value

Data values that can only be true or false, used in logical operations and conditional statements.

object reference

A value that points to the memory location where an object is stored, allowing access to that object.

primitive data type

Basic data types in Java such as int, double, boolean, and char that are not objects.

reference type

A data type that holds a reference (memory address) to an object rather than storing the object's value directly.

relational operator

Operators used to compare values, including ==, !=, <, >, <=, and >=.

See Fiveable more often in Google

Mark Fiveable as a preferred source and Google is more likely to show you our guides in Top Stories, with a "preferred" badge in AI Mode and AI Overviews.

Prefer us on Google