中自動轉換爲雙倍爲什麼Java自動將點數自動加倍?當我施放它時,它會起作用。但在我看來,這不是一種美麗的方式。在參數
我的方法可以更改爲雙倍簽名。但是當我輸入某些東西時,我找不到解決方案
public class Demo {
public static void main(String[] args) {
run(0.1);
run(1*0.1);
run(1);
importedClass.setPosition(0.1, 3.5);
//setPosition(float, float) not applicable for the arguments (double, double)
run((float) 0.1);
run((float) 1*0.1);
run(1);
importedClass.setPosition((float) 0.1, (float) 3.5);
}
public static void run(float x) {
//do something
}
}
拋出未解決的編譯問題:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The method run(float) in the type Demo is not applicable for the arguments (double)
The method run(float) in the type Demo is not applicable for the arguments (double)
at test.Demo.main(Demo.java:6)
因爲'0.1'是一個雙重字面;一個乘以double的整數是一個double等等。如果你想要一個浮點數字,使用'f'後綴。 –