Varargs in method overloading in Java

前端 未结 4 1072
走了就别回头了
走了就别回头了 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 07:57

    In Java, 1 is how you represent an int. It can be either auto-boxed to an instance of Integer or promoted to float, and this explains why the compiler can't decide on the method it should call. But it will never be auto-boxed to Long or Float (or any other type).

    On the other hand, if you write 1F, it is the representation of a float, that can be auto-boxed to a Float (and, in the same spirit, will never be auto-boxed to an Integer or anything else).

提交回复
热议问题