Comparison operator in AP Computer Science A

In AP Computer Science A, a comparison operator (the CED calls them relational operators) is a symbol like ==, !=, <, >, <=, or >= that compares two values and evaluates to a boolean (true or false). With primitives it compares actual values; with objects, == compares references, so you use equals() instead.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is comparison operator?

A comparison operator takes two values and answers one question with a true or false. Java gives you six of them. == and != check whether two values are the same or different (EK 2.2.A.1), and <, >, <=, and >= check how two numeric values relate to each other (EK 2.2.A.2). Every expression built with these operators evaluates to a boolean value (EK 2.2.A.3), which is exactly what if statements and loop conditions need to make decisions.

The one twist that trips everyone up is what == actually compares. For primitive types like int and double, it compares the actual stored values, so 5 == 5 is true. For reference types like String or any object, == compares the object references, meaning it asks "do these two variables point to the exact same object in memory?" not "do these objects hold the same data?" To compare object contents, you call the equals() method instead. The AP CED officially calls these "relational operators," so treat "comparison operator" and "relational operator" as the same thing on the exam.

Why comparison operator matters in AP® Computer Science A

Comparison operators live in Topic 2.2 (Boolean Expressions) inside Unit 2: Selection and Iteration, supporting learning objective 2.2.A, which asks you to write Boolean expressions with relational operators and determine what they evaluate to. They're the foundation the entire unit is built on. An if statement can't select anything and a while loop can't decide when to stop without a boolean expression, and comparison operators are how you build those expressions. Misreading one (like confusing < with <=, an off-by-one error) is one of the most common ways to lose points when tracing code on the multiple-choice section.

How comparison operator connects across the course

== (equality operator) (Unit 2)

The == operator is the comparison operator with a split personality. It does value comparison for primitives but reference comparison for objects. Knowing which behavior you're getting is the single most tested idea about this term.

Object reference (Unit 2)

When you compare two object variables with ==, Java compares their references, not their contents. Two String objects can hold identical text and still fail an == check because they live at different spots in memory. That's why equals() exists.

Null comparison (Unit 2)

Checking whether a reference is null is the one place you should use == with objects, as in if (obj == null). Calling obj.equals(null) on a null reference crashes with a NullPointerException, so the comparison operator is the safe move here.

Boolean expressions and selection (Unit 2)

Comparison operators produce the booleans that if, else if, and loop conditions consume. Every selection and iteration question in Unit 2 starts with you correctly evaluating a relational expression, so this skill compounds through the whole unit.

Is comparison operator on the AP® Computer Science A exam?

Comparison operators show up constantly in multiple-choice code-tracing questions. You'll evaluate a boolean expression, decide which branch of an if runs, or figure out how many times a loop executes (where mixing up < and <= changes the answer by exactly one iteration). On FRQs, no prompt asks you to define the term, but nearly every method you write uses one. A correct if (a.equals(b)) versus a wrong if (a == b) on String or object comparisons is a classic rubric point. Bottom line for the exam: comparing primitives means use == or != freely; comparing object contents means use equals(); checking for null means use ==.

Comparison operator vs equals() method

Both check equality, but they ask different questions. The == comparison operator asks "are these the exact same object (or the same primitive value)?" while equals() asks "do these objects contain the same data?" For primitives, only == works. For Strings and other objects, == compares references and equals() compares contents, so two different String objects with identical text are equals() but not ==. The exam loves this distinction.

Key things to remember about comparison operator

  • Java has six comparison operators (==, !=, <, >, <=, >=), and every expression built with them evaluates to a boolean value.

  • With primitive types like int and double, == compares the actual values stored in the variables.

  • With reference types like String, == compares object references, so use the equals() method to compare what the objects actually contain.

  • The CED's official name for these is relational operators, so expect that wording on the exam.

  • Mixing up < and <= in a loop condition causes off-by-one errors, a favorite trap in code-tracing multiple-choice questions.

  • Use == (not equals()) when checking whether an object reference is null, because calling equals() on null throws a NullPointerException.

Frequently asked questions about comparison operator

What is a comparison operator in AP Computer Science A?

It's an operator that compares two values and evaluates to a boolean. Java has six: == and != check equality, while <, >, <=, and >= compare numeric values. The AP CED calls them relational operators in Topic 2.2.

Can you use == to compare Strings in Java?

Technically yes, but it almost never does what you want. With Strings, == compares object references (whether two variables point to the same object), not the text inside. Use str1.equals(str2) to compare String contents, which is what AP FRQ rubrics expect.

What's the difference between == and equals() in Java?

== is a comparison operator that checks if two primitives have the same value or if two references point to the same object. equals() is a method that checks if two objects contain the same data. For Strings and other objects on the AP exam, equals() is the right tool.

Is a comparison operator the same as a relational operator?

Yes. The AP Computer Science A CED uses the term "relational operators" for ==, !=, <, >, <=, and >=, but teachers and textbooks often say "comparison operators." They mean the same six symbols.

Do comparison operators always return a boolean?

Yes. Per EK 2.2.A.3, any expression built with relational operators evaluates to a boolean value, either true or false. That boolean is what if statements and loop conditions use to decide what happens next.