I have written the code:
int x = 18;
x *= 0.90;
System.out.println(x);
This code printed 16
However, when I wrote
int x = 18;
x = x * 0.90;
System.out.println(x);
it gave me the following error: incompatible types: possible lossy conversion from double to int
I expected both of these code examples to result in the exact same error as x *= y;
is the same as x = x * y;
, but x *= 0.90;
somehow works and x = x * 0.90;
does not. Why is this the case?
*
but not for*=
is entirely on the Java creators, and has nothing to do with Thompson & Ritchie. (P.S. Ritchie died 10+ years ago.) – Shiah