Why initializing String variable with concatenation of some String literal with other String variable which is final behave this way?

huangapple go评论60阅读模式
英文:

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 而不是 == 来比较字符串。但是,请看字符串的身份哈希码。abcdefb 的身份哈希码相同。这就是为什么它们使用 == 是相等的。它们引用了内部缓存中的同一个对象。

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 &quot;==&quot; checks if the two operands reference to the same object. Do not use &quot;==&quot; with any objects including Strings. And note Java Strings are immutable. That mean all the operations with them don&#39;t modify them but create new objects.

</details>



# 答案3
**得分**: -2

这是因为 `b` 等于 `&quot;abc&quot; + def`,而 `def` 等于 `&quot;def&quot;`。因此,实质上你正在比较 `&quot;abcdef&quot;` 和 `&quot;abcdef&quot;`,通过使用 `abcdef == b`。

因此,总的来说,它将*不会*打印任何内容,因为 `&quot;abcdef&quot; == &quot;abcdef&quot;` *总是*评估为 `true`。

<details>
<summary>英文:</summary>

This is happening because `b` is equal to `&quot;abc&quot; + def`, and `def` is equal to `&quot;def&quot;`. So, essentially, you are comparing `&quot;abcdef&quot;` with `&quot;abcdef&quot;` by using `abcdef == b`.

So, in conclusion, it will *not* print nothing, because `&quot;abcdef&quot; == &quot;abcdef&quot;` *always* evaluates to `true`.

</details>



huangapple
  • 本文由 发表于 2020年10月17日 23:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/64403860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定