Possible Duplicate:
Varying behavior for possible loss of precision
int testing = 0;
testing += 2.0
the above code compiles.
where as
int testing = 0;
testing = testing + 2.0;
this code doesn't compile. Any idea why?
Possible Duplicate:
Varying behavior for possible loss of precision
int testing = 0;
testing += 2.0
the above code compiles.
where as
int testing = 0;
testing = testing + 2.0;
this code doesn't compile. Any idea why?
Compound assignment has a hidden cast in Java.
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 value7
because it is equivalent to:short x = 3; x = (short)(x + 4.6);