Variable argument function ambiguity

前端 未结 4 1153
鱼传尺愫
鱼传尺愫 2021-01-12 12:58
   public static void main(String[] args) {
       System.out.println(fun(2,3,4));
     }
   static int fun(int a,int b,int c)
   {
     return 1;
   }
   static int         


        
相关标签:
4条回答
  • 2021-01-12 13:03

    If you test this expressions:

        System.out.println(fun(2,3,4));
        System.out.println(fun(2,3));
        System.out.println(fun(2,3,4,7));
    

    The output is

    1
    0
    0
    

    The Java compiler first checks for a method whose declaration matchs the exact parameters of the invocation, and otherwise, it searches for the alternative method matching.

    0 讨论(0)
  • 2021-01-12 13:09

    Basically there's a preference for a specific call. Aside from anything else, this means it's possible to optimize for small numbers of arguments, avoiding creating an array pointlessly at execution time.

    The JLS doesn't make this very clear, but it's in section 15.12.2.5, the part that talks about a fixed arity method being more specific than another method if certain conditions hold - and they do in this case. Basically it's more specific because there are more calls which would be valid for the varargs method, just as if there were the same number of parameters but the parameter types themselves were more general.

    0 讨论(0)
  • 2021-01-12 13:11

    Compiler always selects the precise method when these kind of ambiguities are encountered. In your scenario func with three arguments is more precise than the variable arguments one so that is called.

    EDIT: Edited as per skeet's comment.

    0 讨论(0)
  • 2021-01-12 13:11

    This behaviour allows the resolution of two overloaded variable argument calls:

    addStates(String... states)
    addStates(Enum... states)
    

    Calling addStates() gives a compile ambiguity problem. Adding in a third method:

    addStates(){
       addStates(new String[0]);
    }
    

    This allows selection selecting the more specific third addStates() to resolve the ambiguity problem of being unsure which of the variable type parameter methods to call.

    0 讨论(0)
提交回复
热议问题