What is the difference between += and =+?

后端 未结 9 1624
夕颜
夕颜 2020-12-03 14:07

What is the difference between += and =+? Specifically, in java, but in general also.

相关标签:
9条回答
  • 2020-12-03 15:07

    The += operation as you said, is used for increment by a specific value stated in the R value.Like,

    i = i+1;
    //is equivalent to 
    i += 1;
    

    Whereas, =+ is not any proper operation, its basically 2 different operators equal and unary plus operators written with each other.Infact the + sign after = makes no sense, so try not to use it.It will only result in a hocum.

    i =+ 1;
    //is equivalent to
    i = +(1);
    
    0 讨论(0)
  • 2020-12-03 15:08

    += is an operator that increments the left-hand side of the assignment by the value of the right-hand side and assigns it back to the variable on the left-hand side. =+ is not an operator but, in fact, two operators: the assignment operator = and the unary plus + (positive) operator which denotes the value on the right-hand side is positive. It's actually redundant because values are positive unless they are negated with unary minus. You should avoid the =+ construct as it's more likely to cause confusion than do any actual good.

    0 讨论(0)
  • 2020-12-03 15:09

    += is get and increment:

    a += 5; // adds 5 to the value of a
    

    =+ isn't really a valid identifier on its own, but might show up when you're using the unary + operator:

    a =+ 5; // assigns positive five to a
    
    0 讨论(0)
提交回复
热议问题