2. String Equality¶
2.1. Objectives¶
- Write code that compares two strings for equality
2.2. Time Goal¶
- 15 minutes on this section
2.3. Common Mistakes¶
- Using ``==`` to test if two strings are equal. This is actually a test to see if they refer to the same object (the same place in memory). Usually you only want to know if they have the same characters in the same order. In that case you should use
equals
orcompareTo
instead. - Treating upper and lower case characters the same in Java. If
s1 = "Hi"
ands2 = "hi"
thens1.equals(s2)
is false.
2.4. Exercises¶
Note
When the operator ==
is used with object variables it returns true when the two variables refer to the same object. With strings this happens when one string variable is set to another or when strings are set to the same string literal.
Note
What is printed when the following code is run?
Note
It prints Bye
since s3 has been assigned to a copy of the value in s2 which is an object reference to the String object that has the characters “Bye” in it.
In addition, s2 == s3
will be true since the two variables refer to the same object. Also, s2.equals(s3)
will also be true, again since the two variables refer to the same object, of course the characters will be the same.
Note
Use equals
to test if two strings have the same characters in the same order. Only use ==
to test if two strings refer to the same object. Most of the time you will want to use equals
and not ==
with strings.
Note
If you use the new
keyword to create a string it will create a new string object. So, even if we create two string objects with the same characters using the new operator they will not refer to the same object. What will the following print?
Note
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 object contain the same characters in the same order.
Figure 2: Two string variables and two string objects that contain the same characters in the same order.
Note
What do you think the following code will print? Run it to check.
Note
Since we used string literals this time rather than the new
keyword, the Java run-time will check if that string literal already exists as an object in memory, and if so reuse it. So s1
and s2
will refer to the same string object. That means that both ==
and equals
will be true.
- 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?
Q-249: Which of the following is true after the code executes?
String s1 = new String("hi");
String s2 = "bye";
String s3 = "hi";
s2 = s1;
- 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 != s2 && s1 == s3
- Because you used the same string literal s1 and s3 will refer to the same object. Since s1 and s2 refer to different string literals they do not refer to the same object.
Q-250: Which of the following is true after the code executes?
String s1 = "hi";
String s2 = "bye";
String s3 = "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.
Q-251: Which of the following is true after the code executes?
String s1 = "hi";
String s2 = "bye";
String s3 = new String("hi");
- I only
- This is true, since s1 and s3 contain the same characters since s1 and s3 actually refer to the same string object. But, it isn't the only thing that is true.
- II only
- This is true since s2 == s1. But, it isn't the only thing that is true.
- III only
- This is true since s3 == s2, and s2 == s1 so it follows that s1 == s3. But, it isn't the only thing that is true.
- II and III only
- This is true since they all refer to the same string object. But, they also contain the same characters so equals is also true.
- I, II, III
- The "equals" operation on strings returns true when the strings have the same characters. The == operator returns true when they refer to the same object. In this case all three references actually refer to the same object so both == and equals will be true.
Q-252: After the following code is executed, which of I, II and/or III will evaluate to true?
String s1 = "xyz";
String s2 = s1;
String s3 = s2;
I. s1.equals(s3)
II. s1 == s2
III. s1 == s3