我已经写了代码:
int x = 18;
x *= 0.90;
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
这段代码打印出来16
然而,当我写下
int x = 18;
x = x * 0.90;
System.out.println(x);
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:incompatible types: possible lossy conversion from double to int
我预计这两个代码示例都会导致与 相同的错误x *= y;,x = x * y;但x *= 0.90;不知何故有效,但x = x * 0.90;无效。为什么会这样呢?