{Tips For Java} Compound assignment operator. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 27

{Tips For Java} Compound assignment operator.

i += j; is not just a shortcut for: i = i + j; But if we try this: int i = 5; long j = 8; Then i = i + j; will not compile but i += j; will compile fine. Because: (JLS) 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. For example, the following code is correct: short x = 3; x += 4.6; and results in x having the value 7 because it is equivalent to: short x = 3; x = (short)(x + 4.6);

2nd Mar 2017, 2:06 AM
Ram chandra Giri
Ram chandra Giri - avatar
3 Answers
+ 5
You should be appreciated for posting concepts like this. Keep doing this. I already am aware of this concept but many are not and this will help them.
2nd Mar 2017, 2:19 AM
Krishna Teja Yeluripati
Krishna Teja Yeluripati - avatar
0
Now that's something I didn't know. Thanks for the information
2nd Mar 2017, 6:37 AM
Jishnu Jayarajan
0
thank you
13th Jun 2018, 6:40 AM
Teddy Koller
Teddy Koller - avatar