+1 vote
in JAVA by
Difference between a = a + b and a += b ?

1 Answer

0 votes
by
The += operator implicitly cast the result of addition into the type of variable used to hold the result. When you add two integral variable e.g. variable of type byte, short, or int then they are first promoted to int and them addition happens. If result of addition is more than maximum value of a then a + b will give compile time error but a += b will be ok as shown below

byte a = 127;

byte b = 127;

b = a + b; // error : cannot convert from int to byte

b += a; // ok

Related questions

0 votes
asked Oct 24, 2020 in JAVA by sharadyadav1986
0 votes
asked May 18, 2020 in JAVA by AdilsonLima
...