Tostring Equals AP CSA
toString and equals Methods in AP CSA: Complete Guide (2025-2026)
The toString and equals methods are core Unit 3 class design skills. toString defines how an object appears as a string, while equals defines what it means for two objects to be logically identical. Both appear frequently on the AP CSA exam and contain several non-obvious traps that cause students to lose easy points.
Table of Contents
The toString Method
Every Java class inherits a toString() from Object. That default returns an unreadable hash like MyClass@1a2b3c. You override it to return something meaningful:
- Signature:
public String toString() - Returns a String (no parameters, no void)
- Called automatically when the object is printed or concatenated with a String
The equals Method
The inherited equals from Object compares references (same as ==). To compare by field values, override it:
- Signature:
public boolean equals(Object other)— the parameter must beObject - Cast
otherto the class type before accessing fields - Compare String fields with
.equals(), not== - Compare primitive fields with
==
On the 2025-2026 AP CSA exam, Unit 3 (Class Creation) accounts for 10–18% of the score. Both toString and equals are explicit learning objectives. Expect at least one MCQ that tests the == vs .equals() distinction for Strings inside an equals method.
Code Examples
Example 1: Writing toString
public class Book {
private String title;
private int pages;
public Book(String title, int pages) {
this.title = title;
this.pages = pages;
}
public String toString() {
return title + " (" + pages + " pages)";
}
public static void main(String[] args) {
Book b = new Book("Dune", 412);
System.out.println(b); // toString called automatically
System.out.println("Read: " + b); // same
}
}
Example 2: Writing equals
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public boolean equals(Object other) {
Point p = (Point) other;
return this.x == p.x && this.y == p.y;
}
public static void main(String[] args) {
Point a = new Point(3, 4);
Point b = new Point(3, 4);
Point c = new Point(1, 2);
System.out.println(a.equals(b)); // same fields
System.out.println(a.equals(c)); // different fields
System.out.println(a == b); // reference comparison
}
}
Example 3: String Field in equals — The Critical Case
public class Badge {
private String code;
public Badge(String code) {
this.code = code;
}
// CORRECT equals:
public boolean equals(Object other) {
Badge b = (Badge) other;
return this.code.equals(b.code); // .equals for String fields
}
public static void main(String[] args) {
Badge x = new Badge("AP101");
Badge y = new Badge("AP101");
System.out.println(x.equals(y)); // should print true
System.out.println(x.code == y.code); // false (different refs)
}
}
Common Pitfalls
This is the single most tested mistake. this.name == other.name compares memory addresses. Two equal strings in different objects fail this test. Always use this.name.equals(other.name).
Writing equals(MyClass other) instead of equals(Object other) does NOT override Object.equals—it creates an overloaded method. The inherited reference-comparison equals is still in play.
return x + y + "" adds x and y numerically first, then converts to String. To concatenate separately, write return "" + x + y or return x + ", " + y.
A void toString() does not override Object.toString(). It is a compile error because void and String cannot both define the same method signature.
== used to compare two objects and asking whether the result is true or false. If an equals override exists and compares fields, the answer may be true even though the objects are different references.Check for Understanding
equals method. IDENTIFY the defect.public boolean equals(Object other) { {
Widget w = (Widget) other;
return this.serial == w.serial;
}
}where serial is a String.toString implementations for a class with int x and int y. WHICH return exactly the string "(3, 7)" when x=3 and y=7?I.
return "(" + x + ", " + y + ")";II.
return "(" + (x + y) + ")";III.
return String.format("(%d, %d)", x, y);
public class Tag {{
private String label;
public Tag(String label) {{ this.label = label; }}
public String toString() {{ return "[" + label + "]"; }}
}}
Tag t = new Tag("urgent");
System.out.println("Status: " + t);
equals method but writes:public boolean equals(Widget other) {{ ... }}What is the problem with this signature?toString are ALWAYS true in AP CSA?I. If you do not write a
toString, Java prints a default hash-code-based string when the object is used in a println.II.
toString must always return the values of all instance variables.III. Concatenating an object to a String automatically calls its
toString.a.equals(b) return? PREDICT.public class Coord {{
private int row, col;
public Coord(int r, int c) {{ row=r; col=c; }}
public boolean equals(Object o) {{
Coord c = (Coord) o;
return row == c.row && col == c.col;
}}
}}
Coord a = new Coord(2, 5);
Coord b = new Coord(2, 5);
toString methods is NOT a valid override of the method inherited from Object?Pair stores two ints: first and second. Its toString is:public String toString() { {
return first + second + "";
}
}What does new Pair(3, 7).toString() return? PREDICT.
Frequently Asked Questions
Yes. System.out.println(obj) and string concatenation obj + '' both invoke obj.toString() automatically. You only need to call .toString() explicitly when you need the String for some other purpose.
Calling other.equals(...) or casting null to your class type when other is null causes a NullPointerException. Robust implementations check for null first. The AP exam typically designs test cases to avoid null inputs, but knowing this is tested in MCQ traps.
Yes, logically: if a.equals(b) is true, then b.equals(a) must also be true. The AP exam may test this by asking whether a given equals implementation is symmetric.
Technically yes, any String is valid. But an empty or null return would be unusual and might cause issues when the result is used. On the exam, return a descriptive String as specified in the problem.
Because the parameter is declared as Object, we need to cast it to access our class's specific fields. The AP exam will not test ClassCastException handling; assume other is always the correct type unless the question says otherwise.
1-on-1 Expert Support
toString and equals confusing you? Work directly with an AP CSA teacher who has helped students achieve a 54% five-rate on the exam.
View Tutoring OptionsRelated Topics
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]