英文:
Why initializing String variable with concatenation of some String literal with other String variable which is final behave this way?
问题
这段代码会打印出 "B"。难道它不应该什么都不打印吗?我读过拼接操作(+)就像执行一个方法,因此会得到一个新的字符串,但这里情况并非如此。我想可能是因为 def
变量被声明为 final,但是对于带有 def
变量的复合操作符并不起作用……
String abcdef = "abcdef";
final String def = "def";
String a = "abc";
a += def;
String b = "abc" + def;
String c = "abc";
c = c + def;
if (abcdef == a) {
System.out.println("A");
}
if (abcdef == b) {
System.out.println("B");
}
if (abcdef == c) {
System.out.println("C");
}
英文:
This code prints "B". Shouldn't it print nothing? I have read that concatenation (+) is like executing a method and therefore it results with new String but here it is not the case. I thought that maybe that's because def variable is final but compound operator with def variable does not work the same way...
String abcdef = "abcdef";
final String def = "def";
String a = "abc";
a += def;
String b = "abc" + def;
String c = "abc";
c = c + def;
if(abcdef == a) {
System.out.println("A");
}
if(abcdef == b) {
System.out.println("B");
}
if(abcdef == c) {
System.out.println("C");
}
答案1
得分: 1
我认为你意识到应该使用 equals
而不是 ==
来比较字符串。但是,请看字符串的身份哈希码。abcdef
和 b
的身份哈希码相同。这就是为什么它们使用 ==
是相等的。它们引用了内部缓存中的同一个对象。
String abcdef = "abcdef";
final String def = "def";
String a = "abc";
a += def;
String b = "abc" + def;
String c = "abc";
c = c + def;
System.out.println("a = " + System.identityHashCode(a)
+ " abcdef = " + System.identityHashCode(abcdef));
System.out.println("b = " + System.identityHashCode(b)
+ " abcdef = " + System.identityHashCode(abcdef));
System.out.println("c = " + System.identityHashCode(c)
+ " abcdef = " + System.identityHashCode(abcdef));
if (abcdef == a) {
System.out.println("A");
}
if (abcdef == b) {
System.out.println("B");
}
if (abcdef == c) {
System.out.println("C");
}
输出
a = 925858445 abcdef = 798154996
b = 798154996 abcdef = 798154996
c = 681842940 abcdef = 798154996
B
英文:
I presume you realize you're supposed to compare Strings with equals
and not ==
. However, look at the identity hashCodes for the strings. abcdef
and b
have the same one. That is why they are equal using ==
They are referencing the same object from the internal cache.
String abcdef = "abcdef";
final String def = "def";
String a = "abc";
a += def;
String b = "abc" + def;
String c = "abc";
c = c + def;
System.out.println("a = " + System.identityHashCode(a)
+ " abcdef = " + System.identityHashCode(abcdef));
System.out.println("b = " + System.identityHashCode(b)
+ " abcdef = " + System.identityHashCode(abcdef));
System.out.println("c = " + System.identityHashCode(c)
+ " abcdef = " + System.identityHashCode(abcdef));
if (abcdef == a) {
System.out.println("A");
}
if (abcdef == b) {
System.out.println("B");
}
if (abcdef == c) {
System.out.println("C");
}
Prints
a = 925858445 abcdef = 798154996
b = 798154996 abcdef = 798154996
c = 681842940 abcdef = 798154996
B
</details>
# 答案2
**得分**: 0
在Java中,运算符"=="用于检查两个操作数是否引用同一个对象。不要将"=="用于任何对象,包括字符串。请注意,Java字符串是不可变的。这意味着对它们的所有操作都不会修改它们,而是会创建新的对象。
<details>
<summary>英文:</summary>
In Java the operator "==" checks if the two operands reference to the same object. Do not use "==" with any objects including Strings. And note Java Strings are immutable. That mean all the operations with them don't modify them but create new objects.
</details>
# 答案3
**得分**: -2
这是因为 `b` 等于 `"abc" + def`,而 `def` 等于 `"def"`。因此,实质上你正在比较 `"abcdef"` 和 `"abcdef"`,通过使用 `abcdef == b`。
因此,总的来说,它将*不会*打印任何内容,因为 `"abcdef" == "abcdef"` *总是*评估为 `true`。
<details>
<summary>英文:</summary>
This is happening because `b` is equal to `"abc" + def`, and `def` is equal to `"def"`. So, essentially, you are comparing `"abcdef"` with `"abcdef"` by using `abcdef == b`.
So, in conclusion, it will *not* print nothing, because `"abcdef" == "abcdef"` *always* evaluates to `true`.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论