更新时间:2023-01-14 21:06:18
a和b是对同一对象(apple2e)的两个不同引用。
strong>因为字符串实现 a
和 b
是不同的引用相同的 code> object。
不幸的是,你的代码不能做你想象的。尝试此操作:
String a =apple2e;
String b =apple2e;
System.out.println(a == b?+ a == b); //false
System.out.println(a == b?+(a == b)); //a == b?true
Java自动实现所有字符串字面量。 是第二个sysout打印它的作用的原因。第一个sysout只打印false
,因为字符串连接( +
)的优先级高于 ==
,因此它等效于:
System.out.println b?apple2e==apple2e);
我不认为这是你想测试的!
另一方面,这将给你两个单独的 String
实例:
String a = new String(apple2e);
String b = new String(apple2e);
System.out.println(a == b?+(a == b)); //a == b?false
$ b
a(reference_id 123)---------------apple2e
b reference_id 456)---------------apple2e
并可以使用 String#intern()
:
String a = new String(apple2e)。intern();
String b = new String(apple2e)。intern();
System.out.println(a == b?+(a == b)); //a == b?true
例如
a(reference_id 123)------ +
+ ---------apple2e
b )------ +
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!!
As I understand it, it's because a and b are two different references to the same object (apple2e).
Because of string interning, and only because of string interning a
and b
are different references to the same String
object.
Unfortunately, your code does not do what you think it does. Try this:
String a = "apple2e";
String b = "apple2e";
System.out.println("a==b? " + a == b); // "false"
System.out.println("a==b? " + (a == b)); // "a==b? true"
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:
System.out.println("a==b? apple2e" == "apple2e");
I don't think that's what you meant to test!
This, on the other hand, will give you two separate String
instances:
String a = new String("apple2e");
String b = new String("apple2e");
System.out.println("a==b? " + (a == b)); // "a==b? false"
Which would schematically look like
a (reference_id 123) --------------- "apple2e"
b (reference_id 456) --------------- "apple2e"
and can be reduced to the original situation using String#intern()
:
String a = new String("apple2e").intern();
String b = new String("apple2e").intern();
System.out.println("a==b? " + (a == b)); // "a==b? true"
e.g.
a (reference_id 123) ------+
+--------- "apple2e"
b (reference_id 456) ------+