当前位置: 代码迷 >> J2SE >> java += 原理解决思路
  详细解决方案

java += 原理解决思路

热度:99   发布时间:2016-04-24 01:05:45.0
java += 原理
有哪位仁兄知道java 中 += 的底层原理吗, 比如类型转换之类等 小弟对原理比较入迷

------解决方案--------------------
不需要理解的太复杂吧?基本上就是自动帮你加个强转而已。

比如:
i += expRight;
约等于
i = (int) (i + expRight);
------解决方案--------------------
Java code
from:java language specification15.26.2 Compound Assignment OperatorsA 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);
  相关解决方案