---
title: "Comparison Operator — AP CSA Definition & Exam Guide"
description: "A comparison operator (==, !=, <, >, <=, >=) compares two values and produces a boolean. Core to AP CSA Topic 2.2 and every if statement and loop you write."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/comparison-operator"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# Comparison Operator — AP CSA Definition & Exam Guide

## Definition

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.

## What It Is

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](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") relate to each other (EK 2.2.A.2). Every [expression](/ap-comp-sci-a/key-terms/expression "fv-autolink") 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](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") instead. The AP CED officially calls these "relational operators," so treat "comparison operator" and "relational operator" as the same thing on the exam.

## Why It Matters

Comparison operators live in Topic 2.2 (Boolean Expressions) inside [Unit 2](/ap-comp-sci-a/unit-2 "fv-autolink"): Selection and Iteration, supporting learning objective 2.2.A, which asks you to write Boolean expressions with [relational operators](/ap-comp-sci-a/key-terms/relational-operator "fv-autolink") 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.

## Connections

### [== (equality operator) (Unit 2)](/ap-comp-sci-a/key-terms/equality-operator)

The `==` operator is the comparison operator with a split personality. It does value comparison for [primitives](/ap-comp-sci-a/key-terms/primitives "fv-autolink") but reference comparison for objects. Knowing which behavior you're getting is the single most tested idea about this term.

### [Object reference (Unit 2)](/ap-comp-sci-a/key-terms/object-reference)

When you compare two [object](/ap-comp-sci-a/key-terms/object "fv-autolink") 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)](/ap-comp-sci-a/key-terms/null-comparison)

Checking whether a [reference](/ap-comp-sci-a/key-terms/reference "fv-autolink") 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.

## On the AP 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 Takeaways

- 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.

## FAQs

### 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.

## Related Study Guides

- [2.2 Boolean Expressions](/ap-comp-sci-a/unit-2/boolean-expressions/study-guide/s6j4i9ram3AlCg3uYjwd)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/comparison-operator#resource","name":"Comparison Operator — AP CSA Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/comparison-operator","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/comparison-operator#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:18.410Z","isPartOf":{"@type":"Collection","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"},"publisher":{"@type":"Organization","name":"Fiveable","url":"https://fiveable.me"}},{"@type":"DefinedTerm","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/comparison-operator#term","name":"comparison operator","description":"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.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/comparison-operator","inDefinedTermSet":{"@type":"DefinedTermSet","name":"AP Computer Science A Key Terms","url":"https://fiveable.me/ap-comp-sci-a/key-terms"}},{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a comparison operator in AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Can you use == to compare Strings in Java?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What's the difference between == and equals() in Java?","acceptedAnswer":{"@type":"Answer","text":"== 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."}},{"@type":"Question","name":"Is a comparison operator the same as a relational operator?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"Do comparison operators always return a boolean?","acceptedAnswer":{"@type":"Answer","text":"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."}}]},{"@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"AP Computer Science A","item":"https://fiveable.me/ap-comp-sci-a"},{"@type":"ListItem","position":2,"name":"Key Terms","item":"https://fiveable.me/ap-comp-sci-a/key-terms"},{"@type":"ListItem","position":3,"name":"Unit 2","item":"https://fiveable.me/ap-comp-sci-a/unit-2"},{"@type":"ListItem","position":4,"name":"comparison operator"}]}]}
```
