## What is the difference between a = a + b and a += b?Provide coding snippet for each of the regular expressions.

Explain detailed difference between a = a + b and a += b. Provide coding snippet for each of the regular expressions in java as an example with output of those snippets.Also try to explain the examples using comments.

#### Posted by Nikheel Bhasmare on October 18th 2017 12:17 PM

Basically a += b means a = a + b but it has 2 different scenarios. Case 1: If data types are same-(check code snippet) a = a + b and a += b will produce same result Case 2: If data types differ (check code snippet) A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.To make a=a+b work in this scenario we need to typecast it as a = (byte)(a+b).

/*Case 1*/

int a 3;

int b 5;

b// it will provide output as 8

+= b//it will also provide output as 8

/*Case 2*/

byte a 3;

int b 5;

+= b// will compile

a+b//won't compile as byte + int = int

= (byte)(a+b); // will compile

#### Posted by anonymous on March 26th 2018 22:31 PM

My chief is also eager of YouTube comic video lessons, he also watch these even in place of work hehehe..

My chief is also eager of YouTube comic video lessonshe also watch these even in place of work hehehe..

• Minimum 20 character
6RTK