compare object references using Boolean expressions in program code
Remember that comparing objects is a little different than comparing primitive type values like int and double.
Objects can be very complex and have many attribute values (instance variables) inside of 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 specifically written equals method to compare all of these values.
In this lesson, we will take a look at String objects and how they are compared with == vs. the equals method.
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 should almost always use equals instead of == to check their equality.
When the operator == is used to compare object variables, it returns true only when the two variables refer to the same object.
These variables are called object references or aliases for the same object.
This happens when one string variable is set to another:
If you run the following code, what will be printed?
ANSWER:
Bye
true
true
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.
What will the following code print?
ANSWER:
false
true
So, keep in mind that unless dealing with aliases, == will be false when comparing two string objects, even if they are the same characters in the same order.
Using the .equals() method will be true if the two string objects contain the same characters in the same order.
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.
Only use == with primitive types like int or to test if two strings (or objects) refer to the same object (aliases).
Make sure to use .equals() with strings to test if they are equal letter by letter.
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 condition is false, it doesn't even have to check the second condition because both parts need to be true.
For example, since s is null in the code below, indexOf throws a NullPointer error for s.
The next screenshot is an example where the if statement avoids the error with short-circuit evaluation.
In other words, because s != null is false, the rest of the boolean expression is not evaluated.
Let's change s to set it to "apple" instead of null in the first line and run the code again:
SUMMARY
Often classes have their own equals method, which can be used to determine whether two objects of the class are equivalent.
Two object reference are considered aliases when they both reference the same object.
Object reference values can be compared, using == and !=, to identify aliases.
A reference value can be compared with null, using == or !=, to determine if the reference actually references an object.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.