a += 10 和 a = a + 之间的区别10 在java中?
a += 10
和 a = a + 10
是否相同,或者它们之间有什么区别?我在学习Java作业时遇到了这个问题。
Are a += 10
and a = a + 10
both the same, or is there some difference between them? I got this question while studying assignments in Java.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如您现在提到的转换...在这种情况下有一个区别:
来自 Java 语言规范 第 15.26.2 节:
有趣的是,他们在规范中给出的示例:
在 Java 中有效,但在 C# 中无效...基本上在 C# 中,编译器执行 += 和 -= 的特殊大小写以确保表达式是要么是目标类型,要么是目标类型范围内的文字。
As you've now mentioned casting... there is a difference in this case:
From the Java Language Specification section 15.26.2:
Interestingly, the example they give in the spec:
is valid in Java, but not in C#... basically in C# the compiler performs special-casing of += and -= to ensure that the expression is either of the target type or is a literal within the target type's range.
没有区别,一个是另一个的简写。甚至编译器也会为两者生成相同的指令。
编辑:正如我刚刚发现的那样,编译器不会为两者生成相同的代码。看看这个:
所以简短的答案是,它们是可以互换的,特别是对于 Java 初学者或任何不担心在最小级别进行优化的人。长答案将取决于我阅读有关 iadd 与 iinc 的内容。
编辑2:好的,我回来了。指令规范(大致)如下:
iadd - 将栈顶的两个整数相加
iinc - 将局部变量增加一个常量
正如我们上面所看到的,我们只要右侧有一个常量,就可以使用 iinc 保存几条指令。
但是如果我们有
a += a
会发生什么呢?那么代码如下所示:
如果我们有
a = a + a
,这与我们得到的结果是一样的。There is no difference, one is shorthand for the other. Even the compiler will generate the same instructions for both.
Edit: the compiler does NOT generate the same code for both, as I just found out. Check this out:
So the short answer, especially for a Java beginner, or anyone who isn't worried about optimizing at the smallest level, is that they are interchangeable. The long answer will depend on me reading about iadd vs iinc.
Edit 2: Ok, I'm back. The instruction specs are (roughly) as follows:
iadd - adds the top two ints on the stack
iinc - increments a local variable by a constant
And as we saw above, we can save a couple of instructions using iinc, as long as there is a constant on the right hand side.
But what happens if we have
a += a
?Then the code looks like this:
which is the same thing we get if we have
a = a + a
.这是在 Java 语言规范第 15.25 节中定义的.2。突出的部分是:
也就是说,在您的情况下,区别在于隐式类型转换:
This is defined in the Java Language Specification, section 15.25.2. The salient part is:
That is, in your case the difference is the implicit type cast:
在您显示的表达式中,它们是等效的,在如下表达式中:
您会了解 += 运算符(以及其他赋值运算符)在哪些情况下有用。如果表达式不平凡,+= 运算符可以防止错误并提高可读性,从而提高可维护性。
In the expressions you show, they are equivalent, in an expression like:
you get an idea in which situations the += operator (and the other assignment operators) is useful. If the expression is non-trivial, the += operator prevents mistakes and improves readability and therefore maintainability.
S/W 领域有一些术语,我可以向您解释一下,
在
a=a+1
中,对a
的赋值是在两步之后测量a
,然后将隔离a
的值分配给左侧a
但在第二种情况下,
a
的值,并直接将10加到a上(这里没有进行单独的复制)。希望这对您有所帮助,还有一件事,我们通常使用
a += 10;
方法,因为它降低了操作成本,正如其他方法一样,There are some terminologies in S/W field, I can explain this to you,
in
a=a+1
assignment fora
is measured after two stepsa
then the value of isolateda
is assigned to left sidea
But in second case,
a
and directly add 10 to a (no isolated copy has been made here).Hope this will be helpful to you, and one more thing, we usually use method that is
a += 10;
because it reduce the cost of operation, as per others one,