The AP Computer Science A 5-hour live stream review is here!Β πŸ’»

Join us on May 5, 2021 for the 🌢️ AP Computer Science A Cram Finale for a last minute review to get all your questions answered!

πŸ“š

All Subjects

Β >Β 

πŸ’»Β 

AP Comp Sci A

Β >Β 

πŸ–₯

Unit 3

3.7 Comparing Objects

3 min readβ€’november 16, 2020

Peter Cao

Caroline Koffke


3.7: Comparing Objects

What it Means When Objects Are Equal

Sometimes, we want to compare objects to see if they are equal. However, using the boolean equality operator may get some unexpected results!
When using the equality operator == to compare two objects, it will only return to true if they refer to the same exact object... not just a copy of that object, but that object itself. Even if the objects have the same exact attributes but are two different instantiations, the equality operator will return false (like identical twins, even though they may look the same, they are still two different people).
Here are some examples with String objects:
String a = "Hi"; String b = "Hi"; String c = a; String d = "Hi!" String e = newString("Hi") System.out.println(a == c); System.out.println(d == b); System.out.println(a == b); System.out.println(a == e);
true false true false

The a.equals(c) Case

In this case, the variable c is used to point to the string at a. That is, c is just another name for a. This means that they are the same string, so the equality to return true.

The d.equals(b) Case

The text in string d is not the same as the text in string b. Because they are different, they are different strings. Because the strings are different, the equality returns false.

The a.equals(b) Case

This one is simple. String a is "Hi" while String b is also "Hi", which is the same. Since the String constructor is not called, a and b are the same string, so the equality returns true.

The a.equals(e) Case

Even though both strings are initialized to "Hi", e uses the String constructor, so the constructor causes e to be a different string from a. Because of this, the equality operator returns false. However, this should return true in most practical situations because in most cases it doesn't matter if they are different strings as long as they say "Hi". This is where the equals() method comes into play.

The Equals Method

Often, we want to check if two objects have the same characteristics, such as two different String objects saying the same text. This is when the equals() method comes in. This comes with all classes in Java (we'll learn about this mysterious Object class in Unit 9!). Unlike the boolean equality operator, the equals operator for most classes returns true if their attributes are the same, even though they aren't the same object (like identical twins!).
We use it with the following syntax, where objectOne is being compared to objectTwo:
objectOne.equals(objectTwo);
Note that the order doesn't matter and one can write this with the same result:
objectTwo.equals(objectOne);
Here is an example illustrating the use of equals():
String a = "Hi"; String b = "Hi"; String c = a; String d = "Hi!" String e = new String("Hi"); System.out.println(a.equals(c)); System.out.println(d.equals(b)); System.out.println(a.equals(b)); System.out.println(a.equals(e));
true false true true

The a.equals(c) Case

In this case, the variable c is used to point to the string at a. That is, c is just another name for a. This means that they are the same string, so the method will return true. This is the same as using the "=" operator.

The d.equals(b) Case

The text in string d is not the same as the text in string b. Because they are different, they are different strings. This will cause the method to return false, unlike the first case. Like the last case, this is the same as using the "=" operator.

The a.equals(b) Case

This one is simple. String a is "Hi" while String b is also "Hi", which is the same. Since the String constructor is not called, a and b are the same string. Moreover, they have the sequence of characters be the same, so the method returns true, the same as above.

The a.equals(e) Case

This one is the one that is different than with the "=" operator. Even though e uses the String constructor, the value that the strings are initialized to are both "Hi", so the method returns true, even though the constructor causes e to be a different string from a.
However, note that this can only be used with objects and not primitive types! For primitive types, we simply need to use the boolean equality operator.

Was this guide helpful?

πŸ’ͺ🏽 Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what you’ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Browse Study Guides By Unit
πŸ™
Exam Reviews
πŸ–±
Unit 10: Recursion
βž•
Unit 1: Primitive Types
πŸ“±
Unit 2: Using Objects
πŸ•Ή
Unit 4: Iteration
βš™οΈ
Unit 5: Writing Classes
⌚️
Unit 6: Array
πŸ’Ύ
Unit 7: ArrayList
πŸ’»
Unit 8: 2D Array
πŸ–²
Unit 9: Inheritance
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now