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

► Click here to show 1 Answer

0 votes
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
Learn More with Madanswer

Related questions

0 votes
asked Oct 24, 2020 in JAVA by sharadyadav1986