Why the casting will cause the error? From my understanding, int num will be implicitly converted to double, but now to avoid this error, I need to do temp.add((double) num);
public void test(int[] nums) {
List<Double> temp = new ArrayList<>();
for (int num: nums) {
temp.add(num);
}
}
Error:
Playground Debug
Line 6: error: no suitable method found for add(int)
temp.add(num);
^
method Collection.add(Double) is not applicable
(argument mismatch; int cannot be converted to Double)
method List.add(Double) is not applicable
(argument mismatch; int cannot be converted to Double)
intwill be converted todouble. That does not mean it will be converted tojava.lang.Double. Boxing and promotion are two different steps. They can’t both happen implicitly.int i = 1; Double d = new Double(i);