---
title: "== Equality Operator — AP CSA Definition & Exam Guide"
description: "The == operator compares two values for equality in Java, returning a boolean. On primitives it checks values; on objects it checks references, a classic AP trap."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/equality-operator"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# == Equality Operator — AP CSA Definition & Exam Guide

## Definition

In AP Computer Science A, == is a relational operator that compares two values and evaluates to a boolean. With primitive types (int, double, boolean) it compares the actual values; with reference types (like String objects) it compares whether two references point to the same object.

## What It Is

The == [operator](/ap-comp-sci-a/unit-1/expressions-and-assignment-statements/study-guide/01dr6uUPDAn3SjtK2Psr "fv-autolink") asks one question. Are these two things equal? It always evaluates to a boolean, `true` or `false`, which is why it shows up constantly inside `if` statements and `while` [loop](/ap-comp-sci-a/key-terms/loop "fv-autolink") conditions. Per EK 2.2.A.1, you can pair it with `!=` (not equal) to test the opposite.

Here's the part the AP exam loves. What "equal" means depends on the type. For [primitives](/ap-comp-sci-a/key-terms/primitives "fv-autolink") like `int`, `double`, and `boolean`, == compares the actual stored values, so `5 == 5` is `true`. For reference types like `String` or any object, == compares the *references*, meaning it checks whether both variables point to the exact same object in memory. Two different `String` objects can hold identical text and still make == return `false`. That's why Java objects get compared with `.equals()` instead, and why mixing those two up is one of the most common point-losers in the course.

## Why It Matters

The == operator lives in Topic 2.2 (Boolean Expressions) in [Unit 2](/ap-comp-sci-a/unit-2 "fv-autolink"): Selection and Iteration, directly supporting learning objective 2.2.A, which asks you to build boolean expressions with [relational operators](/ap-comp-sci-a/key-terms/relational-operator "fv-autolink") and determine what they evaluate to. It's also foundational glue for the rest of the course. Every `if` statement (Topic 2.3 onward), every loop condition, and every search algorithm you write in later units depends on getting equality checks right. If you compare two Strings with == when you meant `.equals()`, your selection logic silently breaks, and on the FRQs that costs points.

## Connections

### Relational operators (Unit 2)

== is one member of the relational operator family alongside `!=`, `<`, `>`, `<=`, and `>=` (EK 2.2.A.2). All of them evaluate to a [boolean](/ap-comp-sci-a/unit-1/variables-and-primitive-data-types/study-guide/rezA6f3hJz84TKaY5Jjl "fv-autolink"), so any of them can sit inside an `if` condition or get combined with `&&` and `||`.

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

Understanding == on [objects](/ap-comp-sci-a/key-terms/object "fv-autolink") requires understanding references. A reference variable stores a pointer to an object, not the object itself, so == on two object variables asks "same address?" not "same contents?" That one mental model explains every == vs .equals() question you'll see.

### [Null comparison (Unit 2)](/ap-comp-sci-a/key-terms/null-comparison)

The one place == is the *correct* tool for objects is checking for [null](/ap-comp-sci-a/key-terms/null "fv-autolink"). Writing `obj == null` is safe, while calling `obj.equals(...)` on a null reference throws a NullPointerException. This pattern shows up in defensive code on FRQs.

### Loop conditions and searching (Unit 2 and beyond)

Equality checks drive `while` loops and linear search. When you search an array of Strings later in the course, the difference between `arr[i] == target` and `arr[i].equals(target)` is the difference between code that works and code that only looks like it works.

## On the AP Exam

Multiple-choice questions test == in two main ways. First, trace questions where you evaluate a boolean expression and pick what prints or which branch runs. Second, trap questions where two Strings or objects with identical contents are compared with == and the answer hinges on knowing it compares references, not contents. No released FRQ centers on == by itself, but nearly every FRQ requires it. Methods that count matches, search for a value, or validate input all need correct equality checks, and graders expect `.equals()` for object contents and == for primitives and null checks. Also watch for the classic typo distractor where a single `=` (assignment) appears where == (comparison) belongs.

## == (equality operator) vs .equals() method

== on objects asks "do these two variables point to the same object in memory?" while .equals() asks "do these two objects have the same contents?" For Strings, `s1.equals(s2)` returns true when the text matches even if they're different objects, but `s1 == s2` can return false for identical text. Rule of thumb for the exam: == for primitives and null checks, .equals() for object contents.

## Key Takeaways

- The == operator always evaluates to a boolean, true or false, which is what makes it usable inside if statements and loop conditions.
- With primitive types like int and double, == compares the actual values, so 7 == 7 is true.
- With reference types like String, == compares references, meaning it only returns true if both variables point to the exact same object.
- Use .equals() to compare the contents of objects and == to compare primitives or to check whether a reference is null.
- A single = is assignment, not comparison, and Java will not let you accidentally use it as a condition the way some languages do (except with booleans, which is a sneaky MCQ trap).
- == pairs with != as the two equality-based relational operators defined in EK 2.2.A.1.

## FAQs

### What is the == operator in Java for AP Computer Science A?

It's a relational operator from Topic 2.2 that compares two values for equality and evaluates to a boolean. With primitives it compares values; with reference types it compares object references (EK 2.2.A.1).

### Does == work on Strings in Java?

It compiles, but it usually doesn't do what you want. == on Strings checks whether two variables reference the same object, so two Strings with identical text can still make == return false. Use .equals() to compare String contents.

### What's the difference between = and == in Java?

A single = assigns a value to a variable, while == compares two values and returns a boolean. Writing `if (x = 5)` is a compile error for an int, but `if (flag = true)` compiles and assigns instead of comparing, which makes it a favorite MCQ distractor.

### When should I use == instead of .equals()?

Use == for primitive types (int, double, boolean, char) and for null checks like `obj == null`. Use .equals() whenever you care about whether two objects, especially Strings, have the same contents.

### Is the == operator on the AP CSA exam?

Yes. It's spelled out in EK 2.2.A.1 under learning objective 2.2.A, and it appears throughout the exam in boolean expressions, if statements, loop conditions, and code-tracing multiple-choice questions.

## 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/equality-operator#resource","name":"== Equality Operator — AP CSA Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/equality-operator","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/equality-operator#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T05:27:20.252Z","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/equality-operator#term","name":"== (equality operator)","description":"In AP Computer Science A, == is a relational operator that compares two values and evaluates to a boolean. With primitive types (int, double, boolean) it compares the actual values; with reference types (like String objects) it compares whether two references point to the same object.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/equality-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 the == operator in Java for AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"It's a relational operator from Topic 2.2 that compares two values for equality and evaluates to a boolean. With primitives it compares values; with reference types it compares object references (EK 2.2.A.1)."}},{"@type":"Question","name":"Does == work on Strings in Java?","acceptedAnswer":{"@type":"Answer","text":"It compiles, but it usually doesn't do what you want. == on Strings checks whether two variables reference the same object, so two Strings with identical text can still make == return false. Use .equals() to compare String contents."}},{"@type":"Question","name":"What's the difference between = and == in Java?","acceptedAnswer":{"@type":"Answer","text":"A single = assigns a value to a variable, while == compares two values and returns a boolean. Writing `if (x = 5)` is a compile error for an int, but `if (flag = true)` compiles and assigns instead of comparing, which makes it a favorite MCQ distractor."}},{"@type":"Question","name":"When should I use == instead of .equals()?","acceptedAnswer":{"@type":"Answer","text":"Use == for primitive types (int, double, boolean, char) and for null checks like `obj == null`. Use .equals() whenever you care about whether two objects, especially Strings, have the same contents."}},{"@type":"Question","name":"Is the == operator on the AP CSA exam?","acceptedAnswer":{"@type":"Answer","text":"Yes. It's spelled out in EK 2.2.A.1 under learning objective 2.2.A, and it appears throughout the exam in boolean expressions, if statements, loop conditions, and code-tracing multiple-choice questions."}}]},{"@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":"== (equality operator)"}]}]}
```
