The Java compiler will first select the method without taking into account any methods of the arity variable, i.e. using int... Only if he does not find suitable methods, he will consider methods with the arity variable. Here long matches, because int can be increased to long by extending the primitive conversion.
JLS, section 15.12.2 defines how the compiler selects the appropriate method:
The rest of the process is divided into three phases to ensure compatibility with Java programming language versions prior to Java SE 5.0. Phase:
- The first phase (Β§15.12.2.2) performs overload resolution without allowing box conversion or decompression or using the method call of the arity variable. If no applicable method is found at this stage, processing continues until the second phase.
This ensures that any calls that were valid in the Java programming language prior to Java SE 5.0 are not considered ambiguous as a result of the implementation of arity variable methods, implicit boxing and / or decompression. However, declaring a method to an arity variable (Β§8.4.1) may change the method chosen to express the method call of this method, since the arity variable method is considered as a fixed arity method in the first phase. For example, declaring m (Object ...) in a class that already declares m (Object) causes m (Object) to no longer be selected for some invocation expressions (such as m (null)), like m (Object [])) is more specific.
- The second phase (Β§15.12.2.3) performs overload resolution when allowing boxing and unpacking, but still eliminates the use of the method invocation of the arity variable. If at this stage no applicable method is found, processing continues until the third phase.
This ensures that the method is never selected by calling the method of the arity variable, if applicable by calling the fixed arity method.
- The third stage (Β§15.12.2.4) allows you to combine overloading using the methods of the variable arity, boxing and unpacking.
Only step 1 is executed here, because long matches.
source share