in JAVA by (31.7k points)
How to find Difference between a = a + b and a += b ?

1 Answer

0 votes
by (6.5k points)
Difference between a = a + b and a += b ?

The += operator implicitly cast the result of addition into the type of the variable used to hold the result. When you add two integral variables e.g. variable of type byte, short, or int then they are first promoted to int and them addition happens. If the result of the addition is more than the maximum value of a then a + b will give a 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

+1 vote
asked Jan 24, 2020 in JAVA by rahuljain1 (6.5k points)
+3 votes
asked May 13, 2021 in JAVA by rajeshsharma (23.9k points)
0 votes
asked May 21, 2020 in JAVA by GeorgeBell (5.2k points)