Ahmed Elmalla - Java Object Equality Explained: == vs .equals() in AP Computer Science A (Complete Guide with Examples) - Your Dedicated Computer Science Tutor | Learn with Kemo
Ahmed Elmalla | AP Computer Science A (Java) Tutor
AP Computer Science A (Java) Tutor Java Programming Tutor (Beginner to Advanced) IGCSE & A-Level Computer Science Tutor Python Programming Tutor for Beginners First lesson available at a discounted rate
Ahmed Elmalla | AP Computer Science A (Java) Tutor

Blog

Java Object Equality Explained: == vs .equals() in AP Computer Science A (Complete Guide with Examples)

Java Object Equality Explained: == vs .equals() in AP Computer Science A (Complete Guide with Examples)

Java == vs .equals() Explained: Understanding Object Equality in AP Computer Science A

One of the most confusing topics for AP Computer Science A students is understanding how Java compares objects.

Many students assume that using == always checks whether two things are equal. That assumption works for primitive data types like int, double, and boolean, but it breaks down when working with objects such as String, ArrayList, or your own custom classes.

During one of my recent AP Computer Science A tutoring sessions, we explored this topic by comparing city names alphabetically, implementing methods with compareTo(), and creating custom Point objects to visualize how Java stores references in memory.

By the end of the lesson, the concepts of reference equality, content equality, and ASCII-based string comparison became much easier to understand.


Why This Topic Appears Frequently in AP Computer Science A

Object comparison is tested in multiple ways throughout the AP CSA curriculum.

Students may need to:

  • Compare two strings correctly.
  • Understand object references.
  • Trace program execution.
  • Predict Boolean expressions.
  • Explain why two seemingly identical objects are not equal.

These concepts also appear naturally inside Free Response Questions where students create classes and compare objects.


Comparing Strings with compareTo()

The lesson began with an exercise comparing city names alphabetically.

Instead of simply returning true or false, the method needed to return whichever city comes first alphabetically.

For example:


 
firstCity("London", "Paris")

should return


 
London

while


 
firstCity("Tokyo", "Cairo")

should return


 
Cairo

This is exactly where Java's compareTo() method becomes useful.


 
city1.compareTo(city2)

returns

  • a negative number if city1 comes first
  • zero if both strings are equal
  • a positive number if city2 comes first

Using this value inside an if-else statement allows us to return the correct city.


How Java Determines Alphabetical Order

Students often ask:

"How does the computer know that London comes before Paris?"

The answer lies in ASCII (Unicode) character values.

Every character has a numeric representation.

For example


 
A = 65

B = 66

C = 67

a = 97

b = 98

Java compares strings character by character using these internal numeric values.

Fortunately, you do not need to memorize ASCII values for the AP exam, but understanding the concept helps explain why compareTo() behaves the way it does.


Java == Versus .equals()

This is one of the biggest misconceptions among beginners.

Consider this code:


 
String a = "Java";
String b = "Java";

Many students think


 
a == b

means

"Do these strings contain the same word?"

Actually, == checks whether both variables point to the same object in memory.

The proper way to compare text is


 
a.equals(b)

which checks the contents of the strings.

This distinction is one of the most commonly tested Java concepts.


Understanding Object References

To make the idea more concrete, we created a custom Point class.


 
Point A = new Point(3,4);

Point B = new Point(3,4);

Although both objects contain identical values,


 
A == B

returns


 
false

because Java created two different objects.

Next we assigned


 
Point C = new Point(7,8);

Point D = C;

Now both variables point to exactly the same object.


 
C == D

returns


 
true

because they reference the same memory location.

Seeing these examples visually helps students understand why Java distinguishes between objects and references.


Building Better Methods

Another focus of the lesson involved improving method design.

Instead of simply returning a Boolean result, students were encouraged to create methods that returned meaningful information.

For example:


 
String firstCity(String city1, String city2)

can return


 
London

Paris

same

depending on the comparison result.

Exercises like this reinforce several AP CSA skills simultaneously:

  • writing methods
  • using return statements
  • conditional logic
  • String methods
  • code readability

Common Student Mistakes

During tutoring sessions I regularly see students make mistakes such as:

❌ Using == to compare strings

❌ Forgetting to return a value from every branch of an if statement

❌ Returning the wrong data type

❌ Confusing object references with object contents

❌ Forgetting that compareTo() returns an integer instead of a Boolean

These mistakes are easy to fix once students understand what Java is actually doing behind the scenes.


Tips for AP Computer Science A Students

If you're studying this topic, I recommend practicing the following:

  • Write methods using compareTo().
  • Compare objects using both == and .equals().
  • Draw memory diagrams showing object references.
  • Predict outputs before running your code.
  • Create your own classes such as Student, Book, or Point and experiment with object references.

These activities strengthen both conceptual understanding and exam performance.


Watch the Complete Lesson

This article is based on one of my real AP Computer Science A tutoring sessions.

In the full lesson, we cover:

  • Java compareTo()
  • String comparison
  • ASCII values
  • == vs .equals()
  • Object references
  • Creating Java classes
  • Memory diagrams
  • AP CSA coding exercises

Watch the Full Lesson

📺 Lesson Recording

(Insert YouTube Link)


Download the Presentation Slides

📊 (Insert Slides Link)


Author Bio

Ahmed Elmalla is a Computer Science educator, Certified Cambridge International AS & A Level Computer Science (9618) teacher, and software engineer with over 20 years of teaching, software engineering, and international tutoring experience. He specializes in AP Computer Science A (Java) and Cambridge IGCSE (0478) and AS & A Level Computer Science (9618), helping students build strong programming, computational thinking, and exam-solving skills. Through his Learn with Kemo platform, he has mentored students from around the world using practical, exam-focused instruction tailored to each learner's needs. His lessons combine real-world software engineering experience with personalized one-to-one tutoring, making complex Java and Computer Science concepts easier to understand. Ahmed is passionate about helping students gain confidence, improve academic performance, and achieve outstanding results in international Computer Science examinations.

 

LinkedIn: https://www.linkedin.com/in/akelmalla

WhatsApp: https://wa.me/60194028484