Skip to main content
Logo image

Section 9.2 Object equality and comparison

Subsection 9.2.1 Reference equality

Watch the following video which shows what happens in memory as primitive types like int and reference types like Dog are compared with == in a physical model of Java memory.
We can also use == or != to test if two reference values, like Turtle and String objects, refer to the same object. In the figure below, we are creating two separate Turtle objects called juan and mia. They do not refer to same object or turtle. Then, we create a reference variable called friend that is set to mia. The turtle mia will have two ways (references or aliases) to name her – she’s both mia and friend, and these variables refer to the same object (same Turtle) in memory. If two reference variables refer to the same object like the turtle on the right in the image below, the test with == will return true which you can see in the code below.
Figure 9.2.1. Turtle Reference Equality

Activity 9.2.1.

What will the code below print out? Try to guess before you run it! Then, add another Turtle friend2 and set it to juan. Does friend2 == juan? Does friend2 == friend? Print out the Boolean expressions for these.

Subsection 9.2.2 Comparing Objects

Comparing objects is a little different than comparing primitive typed values like numbers. Objects can be very complex and have many attribute values or instance variables inside them. For example, the Turtle objects have many instance variables like name, width, height, xPos, yPos, etc. When comparing two Turtle objects, we need a specially written equals method to compare all of these values. In the next sections, we will take a look at String objects and the difference between comparing them with == vs. the equals method.

Subsection 9.2.3 String Equality

The equals method for Strings compares two strings letter by letter. s1.equals(s2) is true if s1 and s2 have all the same characters in the same order. With Strings and other objects, you almost always use equals instead of == to check their equality.
When the operator == or != is used to compare object variables, it returns true when the two variables refer to the same object. These variables are called object references and aliases for the same object. With strings this happens when one string variable is set to another.
Figure 9.2.2. String aliases

Activity 9.2.2.

If you run the following, what will be printed?
The following video traces through the code above and shows how == and equals work with String objects in memory.
Here’s the representation of memory where s2 and s3 refer to the same String object.
Figure 9.2.3. s2 and s3 are aliases referring to the same String object

Subsection 9.2.4 Equality with New Strings

If you use the new keyword to create a string, it will always create a new string object. So, even if we create two string objects with new that contain all the same characters in the same order, they will not refer to the same object.

Activity 9.2.3.

What will the following print? Run the code to see the difference between == and equals with new Strings that are both β€œHello”. Then, write the if statements described below to test the equality of s1 and s3 to see if capitalization matters.
Watch the video below to see how this code works in memory. Since we used the new keyword, two different String objects will be created that each have the characters Hello in them. So s1 == s2 will be false since they don’t refer to the same object, but s1.equals(s2) is true since the two different objects contain the same characters in the same order.
Here is the representation of these String objects in memory.
Figure 9.2.4. Two strings that are equal with equals but not with ==.
Note that you can also create Strings using string literals instead of new, like String s = "Hello". String literals behave a little differently because they are re-used if they already exist instead of creating a new object. But you should not see questions with string literals and == on the AP exam.

Note 9.2.5.

Only use == and != with primitive types like int or to test if two strings (or objects) refer to the same object. Use equals, not == or !=, with strings to test if they are equal letter by letter, and with other objects to see if all of their relevant attributes are equal.

Activity 9.2.4.

Which of the following is true after the code executes?
String s1 = new String("hi");
String s2 = new String("bye");
String s3 = new String("hi");
s2 = s1;
  • s1 == s2 && s1 == s3
  • Do s1 and s3 refer to the same object?
  • s1 == s2 && s1.equals(s3)
  • Yes s2 was set to refer to the same object as s1 and s1 and s3 have the same characters.
  • s1 != s2 && s1.equals(s3)
  • Did you miss that s2 was set to refer to the same object as s1?

Activity 9.2.5.

Which of the following is true after the code executes?
String s1 = new String("hi");
String s2 = new String("bye");
String s3 = new String("hi");
  • s1 == s2 && s1 == s3
  • Do s1 and s2 refer to the same object?
  • s2.equals(s3) && s1.equals(s3)
  • Does s2 have the same characters as s1 or s3?
  • s1 != s3 && s1.equals(s3)
  • s1 and s3 refer to different string objects but they contain the same characters "hi" in the same order.

Activity 9.2.6.

Which of the following is true after the code executes?
String s1 = new String("hi");
String s2 = new String("bye");
String s3 = new String("hi");
  • s1 == s3 && s1.equals(s3)
  • Since s3 uses the new operator it will not refer to the same object as s1.
  • s2.equals(s3) && s1.equals(s3)
  • Do s2 and s3 have the same characters in the same order?
  • !(s1 == s2) && !(s1 == s3)
  • All of the variables refer to different objects. But, s1.equals(s3) would be true since they have the same characters in the same order.

Subsection 9.2.5 Comparing with null

One common place to use == or != with objects is to compare them to null to see if they really exist. Sometimes short-circuit evaluation is used to avoid an error if the object doesn’t exist. Remember that short-circuit evaluation is used with && in Java meaning that if the first part of the if condition is false, it doesn’t even have to check the second condition and it knows the whole && test is false.

Activity 9.2.7.

Try the following code to see a NullPointerException (if you don’t see the exception because of the autograding, you can copy it into the pencil icon scratch area to run it without the grader). Since s is null, trying to access indexOf on s throws an NullPointerException. Comment out the first if statement and run the program again. The second if statement avoids the error with shortcircuit evaluation. Because s != null is false, the rest of the Boolean expression is not evaluated. Now, change s to set it to "apple" instead of null in the first line and run the code again to see that the if statements can print out that β€œapple contains an a”.
The following video shows how the null string reference works in memory.
You have attempted of activities on this page.