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!!
Because of string interning, and only because of string interning
aandbare different references to the sameStringobject.Unfortunately, your code does not do what you think it does. Try this:
Java automatically interns all string literals. That is why the second sysout prints what it does. The first sysout prints only
"false"because string concatenation (+) has higher precedence than==, so it’s equivalent to this:I don’t think that’s what you meant to test!
This, on the other hand, will give you two separate
Stringinstances:Which would schematically look like
and can be reduced to the original situation using
String#intern():e.g.