简体   繁体   English

在Java中使用“ + =”代替“ =”的目的是什么?

[英]What is the purpose of using “+=” instead of “=” in java?

So I ran into this piece of code today while working on an existing codebase. 因此,我今天在处理现有代码库时遇到了这段代码。 My question is why the assignment operator was not used here. 我的问题是为什么这里不使用赋值运算符。

  int getSum(int mVar) {
      int sum = 0;
      sum += ((mVar != null) ? mVar : 0);
      return sum;
    }

It appears an assignment operator would suffice: 似乎赋值运算符就足够了:

int getSum(int mVar) {
      int sum = ((mVar != null) ? mVar : 0);
      return sum;
    }

Is there any benefit to initializing the value to zero? 将值初始化为零有什么好处?

This 这个

int getSum(int mVar) {
    int sum = 0;
    sum += ((mVar != null) ? mVar : 0);
    return sum;
}

is the same as 是相同的

int getSum(int mVar) {
    int sum = 0;
    if( mVar != null ) {
        sum = sum + mVar;
    } else {
        sum = sum + 0;
    }
    return sum;
}

It's just full of shorthand code. 它充满了速记代码。 :) :)

As many of the comments mentioned, a += b is just the same as a = a + b . 正如许多评论所提到的, a += ba = a + b It's a nifty trick to have. 这是一个很漂亮的把戏。 The same also applies for a -= b , a *= b , and a /= b . a- a -= ba *= ba /= b

If you were curious, (mVar != null) ? mVar : 0; 如果您感到好奇, (mVar != null) ? mVar : 0; (mVar != null) ? mVar : 0; is a ternary operator. 是三元运算符。 It goes like condition ? value if true : value if false; condition ? value if true : value if false; condition ? value if true : value if false; and is usually used for assignments. 并且通常用于作业。

Some notes though. 虽然有些笔记。 An int is a primitive data type that can never be null, so the code above is just for the sake of explaining syntax. int是一种原始数据类型,永远不能为null,因此上面的代码只是为了解释语法。

To answer your question, in this example there is no point in using "+=", a regular assignment operator is better 要回答您的问题,在此示例中,没有必要使用“ + =”,常规赋值运算符更好

 int getSum(int mVar) {
      int sum = 0;
      sum += ((mVar != null) ? mVar : 0);
      return sum;
 }

This code is a bad example of "+=" because it shouldn't be used here. 此代码是“ + =”的错误示例,因为此处不应该使用它。 Instead it should be: 相反,它应该是:

   int getSum(Integer mVar) {
          return mVar != null? mvar : 0;
   }

You don't see the benefit of "+=" because sum is already 0 . 您不会看到“ + =”的好处,因为sum已经是0 So, you look at it and you're wondering why you would do sum = 0 + mVar; 因此,您看了一下,就纳闷为什么要这样做sum = 0 + mVar; (which is the same as saying sum += mVar when you can do sum = mVar; (这与说sum += mVar可以做sum = mVar;

A better example is (ofc these values probably don't make sense, but it makes the "+=" valuable): 更好的例子是(ofc这些值可能没有意义,但是使“ + =“有价值):

  int getSum(int mVar) {
          int sum = 5;
          sum += ((mVar != null) ? mVar : 10);
          return sum;
     }

This way if you don't use "+=" you are forced in saying sum = sum + mVar; 这样,如果您不使用“ + =”,那么您将被迫说出sum = sum + mVar; or else it wouldn't be correct. 否则那是不正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM