Equals Method

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.

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

What is the Equals Method?

The equals method is how Java answers the question "do these two objects hold the same value?" Every class 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. 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 the Equals Method matters in AP Computer Science A

The equals method shows up early when you learn String methods and object comparison, and it comes back later when you study inheritance 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.

How the Equals Method connects across the course

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

The equals method is born in the Object class, 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.

Is the Equals Method on the AP Computer Science A 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.

The 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 things to remember about the Equals Method

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

Frequently asked questions about the Equals Method

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