I know this has been covered but I've seen inconsistent arguments here on SO.
So if I have:
String a = "apple2e";
String b = "apple2e";
System.out.println("a==b? " + a == b);
I get FALSE.
As I understand it, it's because a
and b
are two different references to the same object (apple2e
).
So I would have something like:
a (reference_id 123) ------
--------- "apple2e"
b (reference_id 456) ------
Now, if I just want to compare the contents of the two strings, I would use a.equals(b)
Does that mean that the JVM is simply returning if the two references are pointing to the same object? So it's not really doing a character-by-character comparison?
Thanks
EDIT
Hold the phones. Thanks delnan
for pointing out the +
precedence!!!
When I change it to:
System.out.println(a == b);
I indeed get true
.
This makes more sense.
EDIT 2
I can't believe I didn't catch that. lol
I was doing:
"a==b? " + a == b
Which translates to
"a==b? apple2e" == "apple2e"
No wonder it was false!!