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.

Why This Matters for the AP Computer Science A Exam
Relational expressions are the conditions that control every selection statement and loop you write later in this unit. On the multiple-choice section, you will trace code and determine the result of comparisons, and you need to know whether == is comparing primitive values or object references. In free-response code writing, the conditions you put inside if statements and loops start as Boolean expressions, so getting the comparison right is the difference between a method that works and one that loops forever or branches the wrong way.
This topic focuses on relational operators. Logical operators (&&, ||, !), short-circuit evaluation, and equivalent expressions come in later topics, so do not let those distract you from the core skill here: building and evaluating comparisons.
Key Takeaways
- A relational expression always evaluates to a Boolean value (
trueorfalse). ==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.equalswhen 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.
</>Javaint 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:
</>Javaint 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
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:
</>Javaint 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:
</>JavaString 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:
</>JavaString name = null; boolean hasName = (name != null); // false
How to Use This on the AP Computer Science A Exam
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, an assignment will not work as a Boolean condition, so this often shows up as a compile error rather than a wrong answer.
Common Misconceptions
==compares object contents. It does not. For objects,==only checks whether two references point to the same object. Use.equalsto compare contents.==and=are interchangeable. They are not.=assigns a value;==compares values. Using=where you need a comparison is a mistake.- Relational expressions return numbers. A comparison always evaluates to a Boolean value (
trueorfalse), not a number. .equalsand==always give the same answer for Strings. They can differ. Two String objects with identical characters can returnfalsewith==buttruewith.equals.- You need logical operators like
&&and||to make a Boolean expression. A single relational comparison is already a complete Boolean expression. Logical operators and short-circuit evaluation come in later topics.
Related AP Computer Science A Guides
Vocabulary
The following words are mentioned explicitly in the College Board Course and Exam Description 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 >=. |