The difference between += and =+

前端 未结 8 1233
陌清茗
陌清茗 2020-12-01 05:28

I\'ve misplaced += with =+ one too many times, and I think I keep forgetting because I don\'t know the difference between these two, only that one

8条回答
  •  抹茶落季
    2020-12-01 05:40

    a += b is short-hand for a = a + b (though note that the expression a will only be evaluated once.)

    a =+ b is a = (+b), i.e. assigning the unary + of b to a.

    Examples:

    int a = 15;
    int b = -5;
    
    a += b; // a is now 10
    a =+ b; // a is now -5
    

提交回复
热议问题