---
title: "Equals Method — AP Comp Sci A Definition & Exam Guide"
description: "The equals method compares two objects by value, not memory address. Learn how it differs from ==, where it comes from in the Object class, and how AP CSA tests it."
canonical: "https://fiveable.me/ap-comp-sci-a/key-terms/equals-method"
type: "key-term"
subject: "AP Computer Science A"
unit: "Unit 2"
---

# Equals Method — AP Comp Sci A Definition & Exam Guide

## Definition

In AP Computer Science A, the equals method compares two objects for equality based on their contents (like the characters in a String), while the == operator on objects only checks whether two references point to the exact same object in memory.

## What It Is

The equals method is how Java answers the question "do these two [objects](/ap-comp-sci-a/key-terms/object "fv-autolink") hold the same value?" Every [class](/ap-comp-sci-a/unit-3/abstraction-and-program-design/study-guide/o9VgVeIpKRYZ7N7rXfUz "fv-autolink") inherits equals from the Object class, but the inherited version isn't very useful. By default it behaves just like ==, checking whether two references point to the same object. Classes like String override equals to actually compare contents, so `"hello".equals(str)` returns true whenever str contains the characters h-e-l-l-o, no matter where each String lives in memory.

The call looks like `obj1.equals(obj2)` and returns a [boolean](/ap-comp-sci-a/unit-1/variables-and-primitive-data-types/study-guide/rezA6f3hJz84TKaY5Jjl "fv-autolink"). For Strings, this is the comparison you should reach for almost every time. Using == on two String objects can return false even when they contain identical text, because == is asking "same object?" not "same value?" That distinction is one of the most reliably tested ideas in AP CSA.

## Why It Matters

The equals method shows up early when you learn [String methods](/ap-comp-sci-a/unit-1/string-methods/study-guide/SltCtk8JxBIgHcMfG6G4 "fv-autolink") and object comparison, and it comes back later when you study [inheritance](/ap-comp-sci-a/unit-1/objects-instances-of-classes/study-guide/EcpFHGcIKu6385hMohLe "fv-autolink") and the Object superclass, since equals is one of the methods every class inherits from Object. It matters for two big reasons. First, comparing Strings (and other objects) correctly is something you'll do constantly in your own code, including FRQ solutions that search an ArrayList for a matching name or check user input against a target value. Second, understanding why equals exists, and why classes override it, is a concrete example of inheritance and polymorphism in action. Writing `str1 == str2` when you meant `str1.equals(str2)` is one of the classic bugs the multiple-choice section loves to probe.

## Connections

### String Class (Unit 1-2)

String is the class where you actually use equals the most. String overrides equals to compare characters, so two different [String objects](/ap-comp-sci-a/unit-4/recursive-searching-and-sorting/study-guide/tP6n1uldmjMrgteAVspr "fv-autolink") with the same text are equal by .equals but not by ==. String also has equalsIgnoreCase and compareTo for related comparison jobs.

### [Object Class (Unit 9)](/ap-comp-sci-a/key-terms/object-class)

The equals method is born in the [Object class](/ap-comp-sci-a/key-terms/object-class "fv-autolink"), the superclass of every Java class. The Object version just checks reference equality, which is why classes like String override it. When you write your own class and override equals, you're practicing the same inheritance idea Unit 9 is built around.

### Hashcode Method (Unit 9)

hashCode is equals's partner method, also inherited from Object. Java's contract says two objects that are equal by equals should produce the same hash code. AP CSA won't grill you on hash tables, but knowing the pair exists helps the Object class material click.

## On the AP Exam

No released FRQ asks you to define the equals method, but you'll use it constantly. Multiple-choice questions love code segments that compare Strings with == and ask what prints, and the trap answer assumes == compares text. The correct read is that == on objects compares references, so the output may be false even when the Strings look identical. On FRQs, any time your method needs to check whether a String parameter matches a stored value (finding a student by name, checking a label, matching a keyword), write `a.equals(b)`, not `a == b`. Graders expect object comparisons to use equals, and using == there can cost you the point because it's logically wrong, not just stylistic.

## Equals Method vs == operator

The == operator and the equals method both return booleans, but they ask different questions. On objects, == asks "are these two references pointing at the exact same object in memory?" The equals method (when overridden, like in String) asks "do these objects contain the same value?" Two distinct String objects holding "cat" are equal by .equals but can be unequal by ==. For primitives like int and double, == is correct and equals doesn't apply, since primitives aren't objects.

## Key Takeaways

- The equals method compares objects by value or content, while == on objects only checks whether two references point to the same object in memory.
- Always use .equals (not ==) to compare Strings for matching text; == can return false even when two Strings contain identical characters.
- Every class inherits equals from the Object class, and the default version behaves like ==, so classes like String override it to compare contents.
- The call obj1.equals(obj2) returns a boolean, making it perfect inside if statements and loop conditions when searching for a match.
- For primitive types like int, double, and boolean, use ==; equals only applies to objects.
- On FRQs, comparing a String parameter with == instead of .equals is a logic error that can cost you points.

## FAQs

### What is the equals method in Java for AP Computer Science A?

The equals method compares two objects for equality based on their values, returning a boolean. It's inherited from the Object class, and classes like String override it so that `str1.equals(str2)` returns true when both Strings contain the same characters.

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

On objects, == checks whether two references point to the exact same object in memory, while .equals (when overridden, like in String) checks whether the objects hold the same value. Two separate String objects containing "hi" are .equals to each other but may not be == to each other.

### Does == ever work for comparing Strings in Java?

Sometimes, but don't rely on it. Java occasionally reuses String literals, so == can happen to return true, but any String built at runtime (like from concatenation or [input](/ap-comp-sci-a/unit-1/14-assignment-statement-input/study-guide/compoundassignment "fv-autolink")) will usually be a different object. On the AP exam, the safe and correct answer for comparing String text is always .equals.

### Do I use equals to compare ints in Java?

No. Primitives like int, double, and boolean are compared with ==, and that's correct. The equals method only exists on objects, so calling .equals on an int won't even compile.

### Where does the equals method come from?

It's defined in the Object class, the superclass of every Java class, so every object has it automatically. The Object version just compares references like ==, which is why classes such as String override equals to compare actual contents.

## Related Study Guides

- [2.10 Developing Algorithms Using Strings](/ap-comp-sci-a/unit-2/developing-algorithms-using-strings/study-guide/hDOL1VhnMQFPkBf6xMMW)

## Structured Data

```json
{"@context":"https://schema.org","@graph":[{"@type":"LearningResource","@id":"https://fiveable.me/ap-comp-sci-a/key-terms/equals-method#resource","name":"Equals Method — AP Comp Sci A Definition & Exam Guide","url":"https://fiveable.me/ap-comp-sci-a/key-terms/equals-method","learningResourceType":"Concept explainer","educationalLevel":"AP® / High School","about":{"@id":"https://fiveable.me/ap-comp-sci-a/key-terms/equals-method#term"},"audience":{"@type":"EducationalAudience","educationalRole":"student"},"dateModified":"2026-06-11T00:50:32.392Z","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/equals-method#term","name":"Equals Method","description":"In AP Computer Science A, the equals method compares two objects for equality based on their contents (like the characters in a String), while the == operator on objects only checks whether two references point to the exact same object in memory.","url":"https://fiveable.me/ap-comp-sci-a/key-terms/equals-method","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 equals method in Java for AP Computer Science A?","acceptedAnswer":{"@type":"Answer","text":"The equals method compares two objects for equality based on their values, returning a boolean. It's inherited from the Object class, and classes like String override it so that `str1.equals(str2)` returns true when both Strings contain the same characters."}},{"@type":"Question","name":"What's the difference between == and .equals() in Java?","acceptedAnswer":{"@type":"Answer","text":"On objects, == checks whether two references point to the exact same object in memory, while .equals (when overridden, like in String) checks whether the objects hold the same value. Two separate String objects containing \"hi\" are .equals to each other but may not be == to each other."}},{"@type":"Question","name":"Does == ever work for comparing Strings in Java?","acceptedAnswer":{"@type":"Answer","text":"Sometimes, but don't rely on it. Java occasionally reuses String literals, so == can happen to return true, but any String built at runtime (like from concatenation or [input](/ap-comp-sci-a/unit-1/14-assignment-statement-input/study-guide/compoundassignment \"fv-autolink\")) will usually be a different object. On the AP exam, the safe and correct answer for comparing String text is always .equals."}},{"@type":"Question","name":"Do I use equals to compare ints in Java?","acceptedAnswer":{"@type":"Answer","text":"No. Primitives like int, double, and boolean are compared with ==, and that's correct. The equals method only exists on objects, so calling .equals on an int won't even compile."}},{"@type":"Question","name":"Where does the equals method come from?","acceptedAnswer":{"@type":"Answer","text":"It's defined in the Object class, the superclass of every Java class, so every object has it automatically. The Object version just compares references like ==, which is why classes such as String override equals to compare actual contents."}}]},{"@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":"Equals Method"}]}]}
```
