Method oveloading - Object vs Object vararg

后端 未结 2 1779
孤独总比滥情好
孤独总比滥情好 2021-01-19 06:24

See the code below:

// 1st method
private static void method(Object o){
    System.out.println(\"object method\");
}
// 2nd method
private static void method         


        
2条回答
  •  无人及你
    2021-01-19 07:03

    JLS 15.12.2 explains this exact scenario:

    The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase.

    This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing. However, the declaration of a variable arity method (§8.4.1) can change the method chosen for a given method method invocation expression, because a variable arity method is treated as a fixed arity method in the first phase. For example, declaring m(Object...) in a class which already declares m(Object) causes m(Object) to no longer be chosen for some invocation expressions (such as m(null)), as m(Object[]) is more specific.

提交回复
热议问题