Equals Method in AP Computer Science A
In AP Computer Science A, the equals method is a boolean method that compares two objects by their content (like the characters in two Strings or the attributes of two objects), while == only checks whether two references point to the exact same object.
What is the Equals Method?
The equals method answers one question about two objects. Do they hold the same value? It returns a boolean, so s1.equals(s2) is either true or false.
The classic example is the String class. Two separate String objects can both contain "cat" while living at different spots in memory. s1.equals(s2) returns true because the characters match. s1 == s2 asks something different. It asks whether both variables reference the same object.
The CED expands this idea beyond Strings. EK 2.6.B.3 says classes often define their own equals method to set the rules for when two objects count as equivalent, and that equivalency is most often based on the objects' attributes. A Student class might call two objects equal when their studentID and name match. You need to know how to use equals and predict its result. Writing or overriding your own equals method is outside the scope of the AP exam.
Why the Equals Method matters in AP® Computer Science A
Equals shows up in two places in the CED. In Topic 1.15 String Methods (Unit 1), it supports AP Comp Sci A 1.15.B, which asks you to call methods on String objects and determine the result. In Topic 2.6 Equivalent Boolean Expressions (Unit 2), it supports AP Comp Sci A 2.6.B, which asks you to compare object references using Boolean expressions and determine the result. Together those two objectives teach the big idea behind equals. Objects are accessed through references, so "same object" and "same content" are two separate questions. Mix them up and your if statements and loops quietly test the wrong thing. That bug is exactly what the exam likes to hide in code-tracing questions.
Keep studying AP® Computer Science A Unit 2
How the Equals Method connects across the course
The == and != Operators on References (Unit 2)
EK 2.6.B.1 says two variables can hold references to the same object, and == compares those references. Think of == as asking "are these the same house?" and equals as asking "do these houses have the same furniture?" Two different objects with identical content give false for == but true for equals.
String Class and Immutability (Unit 1)
Strings are immutable (EK 1.15.A.3), so methods like substring and concatenation build brand-new String objects instead of changing the old one. That means two Strings with the same text are often separate objects. This is why equals, not ==, is the reliable way to check whether two Strings say the same thing.
compareTo Method (Unit 1)
Both methods compare String content, but they return different things. equals gives you a yes-or-no boolean. compareTo gives you an int that tells you which String comes first alphabetically. Use equals when you care about a match. Use compareTo when you care about order.
Null Checks and De Morgan's Law (Unit 2)
Because equals returns a boolean, it plugs right into compound conditions like obj != null && obj.equals(other). The null check goes first so you never call a method on a reference that points to nothing. Negating the whole condition with De Morgan's law gives obj == null || !obj.equals(other), which is a common equivalence to trace.
Is the Equals Method on the AP® Computer Science A exam?
No released FRQ has used the term "equals method" verbatim, but equals appears constantly in FRQ code you write. Any time you check a String or object for a match inside an if or a loop, graders expect .equals, not ==.
In multiple choice, expect three kinds of questions:
- Code tracing. You get a segment that creates Strings or objects and compares them with both == and equals, and you predict each
true/false. - Reference vs. content. Stems ask which code uses != to check whether two references don't point to the same Student object, or which operator tests whether a reference points to no object at all (that answer is == or != with
null, not equals). - Custom equivalency. A stem describes a Student class compared by
studentIDandnameand asks which approach allows that comparison. The answer is calling an equals method that the class defines using those attributes, which is the EK 2.6.B.3 idea.
Your job is to read each comparison and ask whether it checks identity (==) or content (equals).
The Equals Method vs == operator
The == operator compares object references. It is true only when both variables point to the exact same object in memory. The equals method compares content, so it is true when two objects hold equivalent values, even if they are separate objects. For primitives like int and double, == is correct because there are no references involved. For Strings and other objects, use equals to compare values and save == for identity checks and null checks.
Key things to remember about the Equals Method
The equals method returns a boolean that tells you whether two objects have the same content.
The == operator on objects checks whether two references point to the same object, not whether the objects hold the same values.
To check whether two Strings contain the same characters, use
s1.equals(s2)instead ofs1 == s2.Classes can define their own equals method, and equivalency is usually based on the objects' attributes, like a student's ID and name.
To check whether a reference points to no object, compare it to null with == or !=, because you cannot call equals on a null reference.
Overriding the equals method is outside the scope of the AP exam, but you must be able to call it and predict its result.
Frequently asked questions about the Equals Method
What is the equals method in AP Computer Science A?
It is a boolean method that compares two objects by content. For Strings, s1.equals(s2) is true when both contain the same characters. It is tested in Topic 1.15 and Topic 2.6.
What is the difference between == and .equals in Java?
== checks whether two references point to the same object, and .equals checks whether two objects have equivalent content. Two separate String objects that both hold "hi" are equal by .equals but not necessarily by ==.
Can I use == to compare Strings on the AP exam?
No, not if you want to compare their text. == only compares references, so it can return false for two Strings with identical characters. Use .equals for content and save == for primitives and null checks.
Do I have to write my own equals method for the AP CSA exam?
No. EK 2.6.B.3 says classes often define their own equals method, but overriding equals is explicitly outside the scope of the exam. You only need to call it and figure out what it returns.
How is equals different from compareTo?
equals returns a boolean that answers "same or not?" compareTo returns an int that shows alphabetical order. It is negative, zero, or positive depending on which String comes first. A compareTo result of 0 lines up with equals returning true.
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.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.