有下面的代碼:包裝和自動裝箱
Integer time = 12;
Double lateTime = 12.30;
Boolean late = false;
Double result = late ? lateTime : time; //Why here can I assign an Integer to a Double?
System.out.println(result);
它打印:
12.0
這一個不編譯。爲什麼?
Integer time = 12;
Double lateTime = 12.30;
Double result = time; //Integer cannot be converted to Double
System.out.println(result);
Double result = time + 1.0;還會編譯 –
[Java自動裝箱和三元運算符瘋狂]的可能的重複(http://stackoverflow.com/questions/25417438/java-autoboxing-and-ternary-operator-madness) – Tom