a += 10 和 a = a + 之间的区别10 在java中?

发布于 2024-08-18 03:51:01 字数 90 浏览 2 评论 0原文

a += 10a = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

变身佩奇 2024-08-25 03:51:01

正如您现在提到的转换...在这种情况下有一个区别:

byte a = 5;
a += 10; // Valid
a = a + 10; // Invalid, as the expression "a + 10" is of type int

来自 Java 语言规范 第 15.26.2 节

E1 op= E2 形式的复合赋值表达式相当于
E1 = (T)((E1) op (E2)),其中 TE1 的类型,但 E1 除外 仅评估一次。

有趣的是,他们在规范中给出的示例:

short x = 3;
x += 4.6;

在 Java 中有效,但在 C# 中无效...基本上在 C# 中,编译器执行 += 和 -= 的特殊大小写以确保表达式是要么是目标类型,要么是目标类型范围内的文字。

As you've now mentioned casting... there is a difference in this case:

byte a = 5;
a += 10; // Valid
a = a + 10; // Invalid, as the expression "a + 10" is of type int

From the Java Language Specification section 15.26.2:

A compound assignment expression of the form E1 op= E2 is equivalent to
E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.

Interestingly, the example they give in the spec:

short x = 3;
x += 4.6;

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.

空城旧梦 2024-08-25 03:51:01

没有区别,一个是另一个的简写。甚至编译器也会为两者生成相同的指令。

编辑:正如我刚刚发现的那样,编译器不会为两者生成相同的代码。看看这个:

dan$ cat Test.java
public class Test {
    public static void main(String[] args) {
        int a = 0;
        a = a + 10;
        a += 20;
    }
}

dan$ javap -c Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   bipush  10
   5:   iadd
   6:   istore_1
   7:   iinc    1, 20
   10:  return

}

所以简短的答案是,它们是可以互换的,特别是对于 Java 初学者或任何不担心在最小级别进行优化的人。长答案将取决于我阅读有关 iadd 与 iinc 的内容。

编辑2:好的,我回来了。指令规范(大致)如下:

iadd - 将栈顶的两个整数相加

iinc - 将局部变量增加一个常量

正如我们上面所看到的,我们只要右侧有一个常量,就可以使用 iinc 保存几条指令。

但是如果我们有

a += a 会发生什么呢?

那么代码如下所示:

   7:   iload_1
   8:   iload_1
   9:   iadd
   10:  istore_1

如果我们有 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:

dan$ cat Test.java
public class Test {
    public static void main(String[] args) {
        int a = 0;
        a = a + 10;
        a += 20;
    }
}

dan$ javap -c Test
Compiled from "Test.java"
public class Test extends java.lang.Object{
public Test();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   iconst_0
   1:   istore_1
   2:   iload_1
   3:   bipush  10
   5:   iadd
   6:   istore_1
   7:   iinc    1, 20
   10:  return

}

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:

   7:   iload_1
   8:   iload_1
   9:   iadd
   10:  istore_1

which is the same thing we get if we have a = a + a.

夏末 2024-08-25 03:51:01

这是在 Java 语言规范第 15.25 节中定义的.2。突出的部分是:

复合赋值表达式
形式 E1 op= E2 相当于 E1
= (T)((E1) op (E2)),其中 T 是 E1 的类型,但 E1 是
仅评估一次。

也就是说,在您的情况下,区别在于隐式类型转换:

byte a = 100;
a += 1000;    // compiles
a = a + 1000; // doesn't compile, because an int cannot be assigned to a byte.

This is defined in the Java Language Specification, section 15.25.2. The salient part is:

A compound assignment expression of
the form E1 op= E2 is equivalent to E1
= (T)((E1) op (E2)), where T is the type of E1, except that E1 is
evaluated only once.

That is, in your case the difference is the implicit type cast:

byte a = 100;
a += 1000;    // compiles
a = a + 1000; // doesn't compile, because an int cannot be assigned to a byte.
冷清清 2024-08-25 03:51:01

在您显示的表达式中,它们是等效的,在如下表达式中:

array[getIndex(context)][some / complex + expression] += offset;

您会了解 += 运算符(以及其他赋值运算符)在哪些情况下有用。如果表达式不平凡,+= 运算符可以防止错误并提高可读性,从而提高可维护性。

In the expressions you show, they are equivalent, in an expression like:

array[getIndex(context)][some / complex + expression] += offset;

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.

我不吻晚风 2024-08-25 03:51:01

S/W 领域有一些术语,我可以向您解释一下,

a=a+1 中,对 a 的赋值是在两步之后测量

  1. 系统计算的值a(这里创建了一个新的隔离副本)
  2. 系统将 10 添加到隔离变量a,然后将隔离a的值分配给左侧a

但在第二种情况下,

  1. 系统知道a的值,并直接将10加到a上(这里没有进行单独的复制)。

希望这对您有所帮助,还有一件事,我们通常使用 a += 10; 方法,因为它降低了操作成本,正如其他方法一样,

There are some terminologies in S/W field, I can explain this to you,

in a=a+1 assignment for a is measured after two steps

  1. System calculates the value of a (a new isolated copy is created here)
  2. System add 10 to isolated variable a then the value of isolated a is assigned to left side a

But in second case,

  1. System knows the value of 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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文