Equals Method in AP Computer Science A
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 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.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
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. Writing str1 == str2 when you meant str1.equals(str2) is one of the classic bugs the multiple-choice section loves to probe.
Keep studying AP® Computer Science A Unit 2
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.
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.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.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
Review units, study guides, and course resources.
Check this vocabulary in multiple-choice context.
Apply key concepts in written AP responses.
Estimate the exam score you are working toward.
Review the highest-yield facts before practice.
Put the full course together before test day.