Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Strings can be confusing and seem to work differently than you thought you understood. Let's explore the common misconceptions and remove the confusion.
Workspace Snapshots
Helpful links
- String.equals Javadoc
- Most IDEs generate that code for you. See IntelliJ IDEA (search equals).
- String.intern Javadoc
Where to go from here
In case you weren't aware, we have a Learn Java track. We are working on making it bigger every day. Please come check out the Java Content Trello roadmap to see where we are headed, and make your voice heard!
[MUSIC]
0:00
Hi.
0:04
I'm Craig and I'm a developer.
0:05
Now we've all been there.
0:07
We think we understand the basics of a
programming language and
0:08
then we encounter a line of code that
blows our mind.
0:11
And it makes us question everything we
thought we knew up until that point.
0:14
Well a huge culprit of that sort of
experience is strings in Java.
0:18
Strings have several characteristics about
them that, when combined,
0:23
can definitely go cross-eyed with
confusion.
0:26
And you'll find yourself not alone in your
confusion.
0:28
Hitting the search engines, you'll find
hundreds of thousands of questions about
0:30
them and why they behave the way they do.
0:34
I thought that this format would be a good
way to go and
0:35
explore why strings seem to be so
mysterious.
0:39
And try to demystify and
0:42
expose the common misunderstandings that
happen when we use string in our code.
0:43
Are you ready?
0:46
Let's dive in.
0:48
Okay.
So I've written a little program here
0:50
called Thing.java.
0:52
And it's gonna help us explore some
concepts about strings.
0:53
Now I've added a method here called
explore.
0:57
And it takes an assumption about what we
are going to be exploring and
1:00
an expression.
1:03
The expression is expected to always be
true.
1:04
It will generate a result and let us know
if our assumption is correct.
1:07
So let's get right into some of those
explorations.
1:11
We have explored equality a little bit
already.
1:13
And we know that when we want to compare
primitives together we can use the double
1:16
equal sign.
1:19
So let's make a couple primitives.
1:20
So we'll have int firstNumber equals 12
and int secondNumber equals 12.
1:22
Hey, let's go ahead and call our explorer
method.
1:30
That's what it looks like.
1:32
We say primitives use double equals.
1:33
Okay, and then the expression that we
wanna say,
1:39
wanna say is firstNumber use double equals
to the secondNumber.
1:41
All right, well let's just run this and
1:47
make sure that our explorer method's
working.
1:49
So we'll say clear and then javac.
1:51
We compile Thing.Java, and then we're
gonna run it.
1:53
Cool.
1:59
So we get a yea.
2:00
So we know that it's working.
2:00
So that's primitives, but what about
objects?
2:02
Let's create two brand new objects exactly
the same way.
2:04
Now since we didn't change anything, they
must be equal, right?
2:07
So, let's make two new objects.
2:11
We'll say Object, firstObject equals new
Object.
2:13
And secondObject equals new Object.
2:17
Okay, so let's explore that.
2:26
Well let's say, explore objects work just
like primitives.
2:28
So we should be able to use the double
equals, right?
2:36
[BLANK_AUDIO]
2:38
Let's run that.
2:43
Okay, and we got a what.
2:45
you're assumption is incorrect.
2:47
Our first what.
2:49
So objects work a little bit different.
2:50
When we create an object, the variable
represents an object reference.
2:52
Now you can use double equals for objects,
but what that's saying is that
2:56
the object references are actually
referring to the same object.
3:00
Let's explore that.
3:05
So, first of all, let's fix our
exploration statement here,
3:05
our assumption, and let's say, Objects,
references use double equals to check
3:08
if they refer to the same object in
memory.
3:16
It's gonna wrap seemingly equal objects
are not equal.
3:21
So we can actually use the not equals
sign.
3:28
So let's do that.
3:31
I know, let's go ahead and make a new
object that refers to the first object.
3:33
So we'll just say Object, the sameObject
equals, and
3:38
we'll just literally put the object
reference in there.
3:43
So now let's ex, let's do another
exploration.
3:47
Let's say, let's say explore object
references
3:49
must refer to the same object to use
double equals.
3:54
So, firstObject equals sameObject.
4:02
Let's go ahead and run that and see.
4:05
There we go, two YAYs.
4:10
So this holds true for all objects.
4:11
Now the thing to remember is this,
4:13
strings are objects, everything inherits
from objects.
4:15
So double equals only works if the
variable is referring to the same object.
4:19
So you might have seen some double equals
seemingly work for strings.
4:24
Let's explore that just a bit.
4:28
We'll make a new string, firstString this
equals, [INAUDIBLE] my name and
4:30
we'll say, String secondString is equal to
Craig.
4:37
And let's just go ahead and we'll say
explore
4:42
strings are objects and work like objects.
4:47
So therefore, they're two separate
objects, so they're not the same.
4:53
So we'll say firstString is not equal to
secondString.
4:56
Let's go ahead and run that.
5:05
And we get a what.
5:10
Huh, how could those two be equal?
5:11
Aren't they two completely different
objects?
5:13
What is the deal?
5:15
I thought I understood how this works, and
now I'm not so sure.
5:17
Ok, so
5:20
the good news here is you're not going
crazy, those are actually the same object.
5:20
So here's the thing, when you create a
string like that, you know,
5:25
putting it in double quotes?
5:27
We're creating what is known as a string
literal.
5:29
Now since strings are immutable which
means, if you remember,
5:32
they can't be changed, the compiler does
some tricks to optimize.
5:35
Since it knows the string won't change it
just creates a single instance of that
5:40
string and makes all variables refer to
that single instance.
5:43
It keeps these special references in a
special place in memory called
5:47
the string pool.
5:51
So it's essentially like we saw in the
previous object equality, right?
5:52
They're pointing to the same thing.
5:55
So let's fix our exploration here.
5:56
String literals are actually referring
5:59
to the same object, all right.
6:05
So at compile time that becomes the same
object.
6:11
So we're gonna put double equals there.
6:13
Okay.
6:15
And let's add a new one that uses a
dynamic string,
6:16
one that isn't there at compile time.
6:18
So you can kind of imagine this as input
coming in from the user.
6:20
Now a handy way to make sure that we're
creating a unique string is
6:24
by using its constructor that actually
takes a string.
6:27
So let's take a look at that.
6:30
So let's say, string differentString is
equal to a new String.
6:32
They have the same, what seems like to be
the same value, so let's do this.
6:41
All right, this exploration string objects
6:45
that contain the same characters but
6:51
point to different objects cannot use
double equals.
6:55
So let's prove that really quick.
7:03
FirstString does not equal a
differentString.
7:05
Okay, so let's check those assumptions.
7:13
Awesome, those are both YAY.
7:18
So, one final trick that you might see
used is a process called string interning.
7:20
You can actually take a user imputed
string and add it to the same string pool,
7:25
and you do that by using a method on
stings called intern.
7:30
So let's explore that, just for a quick
second.
7:33
So let's add a string called
anotherString.
7:36
And we use that same constructor method,
okay.
7:40
And let's make a new assumption and we'll
say string interning
7:47
adds to the same String Pool where
literals live,
7:53
so you get back the same reference, if it
exists.
8:00
Okay, so we can say anotherString and
8:06
if we do .intern is equal, now we can use
double equals.
8:11
Let's go ahead and make sure that that
works.
8:18
Awesome.
8:22
So because you almost always are checking
object equality and
8:25
not concerning yourself with object
reference equality,
8:28
this is why you almost always should use
the equals method, like this.
8:31
[BLANK_AUDIO]
8:34
And this is true of all objects, so
8:40
all objects should use equals to check
equality.
8:43
So we'll say firstString equals
differentString.
8:48
And you remember different string was the
one that was created with the new.
8:55
So see differentString was created with
the new string, Craig, and
9:01
the first string was a literal.
9:04
So they're two separate, they're in,
they're in different memory pools.
9:06
Let's just make sure that worked.
9:09
YAY.
9:12
So how does that equals method work?
9:14
Well for strings,
9:16
it must check that every single character
is the same in the other string.
9:16
So the way this works is that string has
overridden the equals method
9:21
on this class definition.
9:24
Now I have added a link in the teacher's
notes about how you might want to go about
9:25
writing your own equals method in your own
classes.
9:29
All right, let's review the output of your
exploration here together one more time.
9:31
So primitives use double equals.
9:36
Object references use double equals to
check if they refer to the same object in
9:38
memory, seemingly equal objects are not
equal.
9:42
Object references must refer to the same
object to use double equals.
9:46
So that's the only time you should use
double equals.
9:49
Now string literals are actually referring
to the same object.
9:52
Remember they go into the string pool
together.
9:56
String objects that contain the same
characters but
9:58
point to different objects can not use
double equals.
10:01
String interning adds to the same string
pool where literals live.
10:04
So you can get back the same reference, if
it exists of course.
10:08
And, all objects should use equals to
check for equality, and
10:11
you can write your own.
10:15
So I hope this knowledge will arm you,
10:16
as you come up against confusing
encounters with strings.
10:18
And help remind you that you are, in fact,
not going crazy.
10:21
So there you go.
10:27
I hope that cleared things up for you and
10:28
if nothing else it should give you
something to refer back to
10:30
if you do in fact end up thinking you're
losing your mind about strings.
10:33
If you like this format and
10:37
would like to see more like this please
speak up in the forum.
10:38
And we'll tackle some more of the more
nuance problems that
10:40
all we developers encounter from time to
time.
10:43
As always, your suggestions are very
welcome and
10:46
really help to shape what we're building
together here.
10:48
Thanks for hanging out, and see you soon.
10:51
You need to sign up for Treehouse in order to download course files.
Sign up