日期:2014-05-20  浏览次数:20653 次

java += 原理
有哪位仁兄知道java 中 += 的底层原理吗, 比如类型转换之类等 小弟对原理比较入迷

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

比如:
i += expRight;
约等于
i = (int) (i + expRight);
------解决方案--------------------
Java code

from:java language specification

15.26.2 Compound Assignment Operators
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);