I have the following Java code:
double i = 4.2;
int x = 42;
x += i; // A valid statement
x = x + i; // An invalid statement
x += i
seems to give no errors, and sets the value of x
to 46
, whereas x = x + i
(correctly) gives a compilation error:
Error:(30, 15) java: incompatible types: possible lossy conversion from double to int
Clearly there is a lossy conversion in x += i
too. Why is that statement valid?