带有 charAt() 方法的 String 上的 Java += 运算符导致 char 添加
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24938504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java += operator on String with charAt() methods results in char addition
提问by jodles
I'm working through the problems on Codingbat.com and bumped into the fact that for the +=
operator, a += b
isn't necessarily exactly equal to a = a + b
. This is well known and has been discussed on SO before, but I bumped into something strange happening when using the charAt()
method combined with the above syntax that I can't make sense of.
我正在解决 Codingbat.com 上的问题,并发现对于+=
操作员来说,a += b
不一定完全等于a = a + b
. 这是众所周知的,并且之前已经在 SO 上讨论过,但是在使用该charAt()
方法与我无法理解的上述语法相结合时,我遇到了一些奇怪的事情。
Say I have two variables:
假设我有两个变量:
String str = "The";
String result = "";
I want to add the first letter in "str" two times to "result". One way of doing this is to:
我想将“str”中的第一个字母两次添加到“result”中。这样做的一种方法是:
result = result + str.charAt(0) + str.charAt(0);
which results in result = "TT"
.
这导致result = "TT"
.
However, if I use the +=
operator, e.g.:
但是,如果我使用+=
运算符,例如:
result += str.charAt(0) + str.charAt(0);
I get result = "168"
. Clearly character addition has taken place (ASCII code for 'T' is 84, 84*2 = 168).
我明白了result = "168"
。显然已经发生了字符添加('T' 的 ASCII 码是 84,84*2 = 168)。
I'm wondering what is actually going on in the first case using the +=
operator. According to the docs on assignment operators: E1 op= E2
is equivalent to E1 = (T)((E1) op (E2))
. So I would expect the latter expression to output "168"
just like using the +=
operator did. But the following outputs "TT"
correctly, and not "168"
:
我想知道在使用+=
运算符的第一种情况下实际发生了什么。根据赋值运算符的文档:E1 op= E2
相当于E1 = (T)((E1) op (E2))
. 所以我希望后一个表达式"168"
像使用+=
运算符一样输出。但以下输出"TT"
正确,而不是"168"
:
result = (String)(result + str.charAt(0) + str.charAt(0));
Have I just misunderstood the documentation? I also found an answeron SO that suggests that str1 += str2
is equivalent to:
我只是误解了文档吗?我还在SO 上找到了一个答案,表明这str1 += str2
相当于:
str1 = new StringBuilder().append(str1).append(str2).toString();
But evaluating the following:
但评估以下内容:
result = new StringBuilder().append(str.charAt(0)).append(str.charAt(0)).toString();
Still results in "TT"
, not "168"
.
结果仍然是"TT"
,不是"168"
。
Sorry for the long post! I'm just curious at what actually is happening when using charAt()
in combination with a String and +=
, because the two "equivalents" I've found (if I've translated them from two to three terms correctly) does not produce the same result.
抱歉,帖子太长了!我只是好奇charAt()
与 String and 结合使用时实际发生了什么+=
,因为我发现的两个“等价物”(如果我正确地将它们从两个术语翻译成三个术语)不会产生相同的结果。
采纳答案by SamYonnou
As you stated in your question E1 op= E2
is equivalent to E1 = (T)((E1) op (E2))
. So the line
正如您在问题中所述,E1 op= E2
相当于E1 = (T)((E1) op (E2))
. 所以线
result += str.charAt(0) + str.charAt(0);
is equivalent to
相当于
result = (String) ((result) + (str.charAt(0) + str.charAt(0)));
(Note the parentheses around the char addition, they are important)
(注意字符添加周围的括号,它们很重要)
This will evaluate to
这将评估为
result = (String) (("") + ('T' + 'T'));
result = (String) (("") + (168));
result = (String) ("168");
result = "168";
whereas without parentheses you get
而没有括号,你会得到
result = (String) ("" + 'T' + 'T');
result = (String) ("T" + 'T');
result = (String) ("TT");
result = "TT";
回答by arshajii
result = result + str.charAt(0) + str.charAt(0);
result
is a string, so the first +
performs string concatenation, producing another string. The second +
, by the same logic, will also perform string concatenation. This gives the desired result.
result
是一个字符串,所以第一个+
执行字符串连接,产生另一个字符串。第二个+
,按照相同的逻辑,也将执行字符串连接。这给出了所需的结果。
result += str.charAt(0) + str.charAt(0);
The right-hand side, str.charAt(0) + str.charAt(0)
, is evaluated first. Now, you're adding two characters, which works like simple integer addition, adding the ASCII values. We then append this result (an int
-- 84 in this case) to result
, which is what you see.
str.charAt(0) + str.charAt(0)
首先评估右侧的。现在,您要添加两个字符,其工作方式类似于简单的整数加法,即添加 ASCII 值。然后我们将此结果(int
在本例中为-- 84)附加到result
,这就是您所看到的。
char c = 'T';
System.out.println(c + c);
168
The underlying difference here is just the order in which things are evaluated.
这里的根本区别只是评估事物的顺序。
回答by njzk2
str.charAt(0) + str.charAt(0)
is evaluated as a whole before the assignment +=
takes place.
在分配+=
发生之前作为一个整体进行评估。
charAt
returns a char
, on which addition is defined like on integers. Therefore, this is the same as :
charAt
返回 a char
,在其上定义加法,就像在整数上一样。因此,这与:
result = result + (str.charAt(0) + str.charAt(0));