我已经编写了代码:
int x = 18;
x *= 0.90;
System.out.println(x);
此代码打印了16
然而,当我写下
int x = 18;
x = x * 0.90;
System.out.println(x);
它给出了以下错误:incompatible types: possible lossy conversion from double to int
我预计这两个代码示例都会导致完全相同的错误,因为x *= y;
与x = x * y;
相同,但x *= 0.90;
以某种方式工作,而x = x * 0.90;
不能.为何会是这样呢?