开发者

What is the difference between a += b and a =+ b , also a++ and ++a?

开发者 https://www.devze.com 2023-02-13 14:44 出处:网络
As I mentioned in the开发者_运维技巧 title, What is the difference between a += b and a =+ b , alsoa++and++a ? I\'m little confuseda += b is equivalent to a = a + b

As I mentioned in the开发者_运维技巧 title,

What is the difference between a += b and a =+ b , also a++ and ++a ? I'm little confused


a += b is equivalent to a = a + b

a = +b is equivalent to a = b

a++ and ++a both increment a by 1. The difference is that a++ returns the value of a before the increment whereas ++a returns the value after the increment.

That is:

a = 10;
b = ++a; //a = 11, b = 11

a = 10;
b = a++; //a = 11, b = 10


a += b is equivalent to a = a + b

a = +b is equivalent to a = b

a++ is postfix increment and ++a is prefix increment. They do not differ when used in a standalone statement, however their evaluation result differs: a++ returns the value of a before incrementing, while ++a after. I.e.

int a = 1;
int b = a++; // result: b == 1, a == 2
int c = ++a; // result: c == 3, a == 3


Others have covered the answers to most of your questions. However, they are missing a bit about your second example.

a = +b assigns the value of +b to a. The "unary plus" is a no-operation for numeric types, but a compile-time error on other types of objects (for example, you can't use it with a string). It is provided mainly so you can write numbers with a leading + sign when you want to. This is never necessary, but it can improve readability in some circumstances.


a+=b ========> a=a+b

a=+b ========> a=b

++a will increment the variable and return the incremented value.

a++ will increment the variable but return the value before it was incremented.


Java operators

a += b;  // a = a + b
a = +b;  // a = b
a++;     // a = a + 1 (returning a if used inside some expression)
++a;     // a = a + 1 (returning a + 1 if used inside some expression)


a += b <=> a = a + b
a =+ b <=> a = b
a++ // post increment, means the value gets used, and after that, a is incremented by one
++a //pre increment, a is incremented by one before the value is used


a++ first reads the value of a and then increments its value. ++a first increments the value and then reads it. You can see easily the difference printing them.

int a = 4;
System.out.println(a++); // prints 4, after printing, a == 5
System.out.println(++a); // first increments a, then reads its value (6), and that's what got printed.

for a += b and a = +b, @Péter Török has answered clearly before.


  • a += b; is equivalent to a = a + b;.

  • a =+ b; is equivalent to a = +b;. This means +b (positive) is assigned to variable a.

  • a++ is post increment of variable a, meaning the value of the variable is used before incrementing by 1.

  • ++a is pre-increment of variable a, meaning the value of the variable is incremented by 1 and used after increment.


You can find the difference here There are examples for all the cases you mention!

0

精彩评论

暂无评论...
验证码 换一张
取 消