0 votes
in JAVA by
How to find Difference between a = a + b and a += b ?

1 Answer

0 votes
by
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
+3 votes
asked May 13, 2021 in JAVA by rajeshsharma
...