0

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)
4
  • 5
    Yes, int will be converted to double. That does not mean it will be converted to java.lang.Double. Boxing and promotion are two different steps. They can’t both happen implicitly. Commented Jan 15, 2020 at 5:14
  • Sometimes when you run it on debug BEFORE running it on "normal run mode" this will happen, run it normal and then try debug Commented Jan 15, 2020 at 5:14
  • @VGR I see, and the reason why the following works is because it only has promotion one step right? int i = 1; Double d = new Double(i); Commented Jan 15, 2020 at 5:20
  • Yes, that’s correct. Commented Jan 15, 2020 at 5:22

5 Answers 5

6

You have a list of Doubles, and you're trying to add an int to it...

This statement

temp.add(num);

Will first need to box num to its wrapper form, which is java.lang.Integer, before adding it to the list (because the list takes reference types, not primitives).

What happens next is that you're trying to add an Integer to a collection of Doubles. And this is not allowed. Integer is not a subclass of Double. These are sibling types (they both are subclasses of Number), but Double is not compatible with Integer.

From my understanding, int num will be implicitly converted to double

When the target is double, yes, the number will be promoted. But if the target is Double, the compiler refuses to do this implicitly (it would have to box your int to Integer before making it a Double, and that fails there).

If you want a quick fix, you can use double as the type for num:

for (double num: nums) {
    temp.add(num);
}
Sign up to request clarification or add additional context in comments.

3 Comments

In that case, shouldn't error show up as "Integer cannot be converted to Double" instead of "int cannot be converted to Double"?
@weijia_yu It doesn't matter, does it? I've typed the steps that the compiler would take to make the call compatible, but that is not what it does. There's simply no relationship between int and Double, and that's what the compiler reports. Further, wouldn't it be confusing if the compiler said Integer cannot be converted to Double? You would surely wonder where you'd typed Integer.
very well explained @ernest_k !
0

Definitely true since the signature method boolean add(E e); in List interface uses a generic type declaration.

Thus, if you already declare List<Double> type, then all elements should be implicitly implied as same type of Double, especially the error will be thrown at compiling time.

See more the specification of List interface at https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/List.html

To fix the issue, just use explicitly casting to double

temp.add((double)num);

Best,

Comments

0
public void test(int[] nums) {
    List<Double> temp = new ArrayList<>();
    for (int num: nums) {
        temp.add(num * 1.0);
    }
}

OR

public void test(int[] nums) {
    List<Double> temp = new ArrayList<>();
    for (double num: nums) {
        temp.add(num);
    }
}    

Comments

0

From my understanding, int num will be implicitly converted to double

Not implicitly, but only when we specify the compiler to convert by either casting or assigning. (There are answers above which have the casting part, so I am not including an example again.) For assignment, here is one:

int intNumber = 0;
double doubleNumber = intNumber; //This will result in conversion from int to double

Comments

-1

you can convert int to string then convert it to double

 public void test(int[] nums) {
            List<Double> temp = new ArrayList<>();
            for (int num: nums) {
                temp.add(Double.valueOf(num));
            }

        }

1 Comment

An answer consisting of code with no explanation is not a good answer. And using a String is a very poor way to convert an int to a double. And, the String conversion isn’t necessary—parseDouble(num) would work just fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.