Compiler error : reference to call ambiguous

前端 未结 6 1844
無奈伤痛
無奈伤痛 2020-11-29 09:07

Case 1

static void call(Integer i) {
    System.out.println(\"hi\" + i);
}

static void call(int i) {
    System.out.println(\"hello\" + i);         


        
6条回答
  •  眼角桃花
    2020-11-29 10:00

    from JLS 15.12.2.2

    JLS 15.12.2.2 Choose the Most Specific Method

    IIf more than one method declaration is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen. The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

    neither of these methods can be passed to the other (the types for int[] and Integer[] arent related) hence the call is ambiguous

提交回复
热议问题