Varargs in method overloading in Java

前端 未结 4 1071
走了就别回头了
走了就别回头了 2020-11-29 07:40

The following code doesn\'t compile.

package varargspkg;

public class Main {

    public static void test(int... i) {
        for (int t = 0; t < i.lengt         


        
4条回答
  •  佛祖请我去吃肉
    2020-11-29 08:00

    Why is in this case the error as in the first case not reported? It appears that auto-boxing and automatic type promotion are both applied here. Is auto-boxing applied first the error is resolved?

    Just an opinion - in case of varargs the JVM actually has to create an array of arguments. In case of Integer and Float, it's obvious what type of array it should create. So, it probably might be the reason for no ambiguity error.

    But still, it's kind of confusing, why it can't create an array of Integers, when by default 1, 3 are ints.

    Looks like this has been discussed here in SO in the past bug with varargs and overloading? and is infact a bug, according to the discussion.

提交回复
热议问题