πŸ“š

Β >Β 

πŸ’»Β 

Β >Β 

πŸ–₯

3.7 Comparing Objects

4 min readβ€’june 18, 2024

Avanish Gupta

Avanish Gupta

Milo Chang

Milo Chang


AP Computer Science AΒ πŸ’»

130Β resources
See Units

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 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 = new String("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 with a and c. 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 same sequence of characters, so the method returns true, the same as when we used the "==" operator.
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 the equals method can only be used with objects and not primitive types! For primitive types, we simply need to use the boolean equality operator.
When you're comparing strings and other objects, you almost always want to use the equals method. The only reason you should use the "==" operator is if a string or object has to be the exact same string or object you're checking against.
Browse Study Guides By Unit
βž•Unit 1 – Primitive Types
πŸ“±Unit 2 – Using Objects
πŸ–₯Unit 3 – Boolean Expressions & if Statements
πŸ•ΉUnit 4 – Iteration
βš™οΈUnit 5 – Writing Classes
⌚️Unit 6 – Array
πŸ’ΎUnit 7 – ArrayList
πŸ’»Unit 8 – 2D Array
πŸ–²Unit 9 – Inheritance
πŸ–±Unit 10 – Recursion
🧐Exam Skills

Fiveable
Fiveable
Home
Stay Connected

Β© 2024 Fiveable Inc. All rights reserved.


Β© 2024 Fiveable Inc. All rights reserved.