[英]When varargs started to not conflict with no-arg?
Today I found that the following code compiles and runs with no any warning:今天我发现以下代码编译并运行时没有任何警告:
public class Try_MultipleArguments2 {
public static void main(String[] args) {
myfunction();
myfunction(1, 2, 3);
}
public static void myfunction(int ... as) {
System.out.println("varags called");
}
public static void myfunction() {
System.out.println("noarg called");
}
}
I am remembering clear, that it was not so earlier.我记得很清楚,不是那么早。
Is this JVM change or my memory glitch???这是 JVM 更改还是我的内存故障???
How it distinguish between no-arg and varargs?它如何区分无参数和可变参数?
UPDATE更新
The following code also runs ok:以下代码也可以正常运行:
public class Try_MultipleArguments2 {
public static void main(String[] args) {
myfunction();
myfunction(1, 2, 3);
}
public static void myfunction(int ... as) {
System.out.println("varags called");
}
// public static void myfunction() {
// System.out.println("noarg called");
// }
}
These are overloaded methods.这些是重载方法。 The compiler knows which method the compiled
main
shoulld call from the method signature.编译器知道编译的
main
应该从方法签名中调用哪个方法。 See this specification :请参阅此规范:
When a method is invoked (§15.12), the number of actual arguments (and any explicit type arguments) and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked (§15.12.2).
调用方法时(第 15.12 节),在编译时使用实际参数(和任何显式类型参数)的数量和参数的编译时类型来确定将被调用的方法的签名(第 15.12.2 节)。
Furthermore, the method chosen is the one that is most specific .此外,所选择的方法是最具体的方法。 See this .
看到这个。 In this case, the no-arg method is more specific than the varargs version - again the number of parameters is checked to see which method to choose..
在这种情况下,无参数方法比可变参数版本更具体 - 再次检查参数数量以查看选择哪种方法。
Its function overloading at backend.它的功能在后端重载。
Your void myfunction(int ... as)
is accepting multiple arguments while your void myfunction()
has no argument.您的
void myfunction(int ... as)
接受多个参数,而您的void myfunction()
没有参数。 I don't see any glitch in this .我看不出这有什么问题。 Method Overloading
方法重载
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.