Why don't Java's +=, -=, *=, /= compound assignment operators require casting?

后端 未结 11 1320
暖寄归人
暖寄归人 2020-11-21 05:06

Until today, I thought that for example:

i += j;

Was just a shortcut for:

i = i + j;

But if we try this:<

11条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 05:40

    Yes,

    basically when we write

    i += l; 
    

    the compiler converts this to

    i = (int)(i + l);
    

    I just checked the .class file code.

    Really a good thing to know

提交回复
热议问题