[英]Varargs and argument less method
I am just catching up with java 1.5, (yes i know its too early;) ) . 我只是赶上Java 1.5,(是的,我知道还为时过早)。 while trying out few exercises on varargs , i just found something strange as below.
在尝试对varargs进行一些练习时,我发现了如下奇怪的地方。 the code compiles well and the varargs method is invoked only when i supply atleast one parameter.
代码编译良好,仅当我提供至少一个参数时才调用varargs方法。 shouldn't this have been compiler error, a method and overloaded method with varargs.
这不应该是编译器错误,使用varargs的方法和重载方法。 Or is there any specific usecase you may think, this scenario will be useful
还是您可能认为有任何特定的用例,这种情况将很有用
public class VarargsExample {
public static void main(String args[]) {
test1();
}
public static void test1(int... x) {
System.out.println("AssertionExample.test1(ARRAY METHOD)");
}
public static void test1() {
System.out.println("AssertionExample.test1(PARAM LESS)");
}
}
PS: tried to search this in SO, could not find similar one. PS:试图在SO中搜索此内容,但找不到类似的内容。 pardon me if there is one already:)
请原谅我:)
Summary, thanks all for your quick responses. 总结,谢谢大家的快速反应。 seems to be the normal methods are the one preferred.
似乎是正常的方法是一种首选。 Same is the case when a single param method is present as below
如下所示存在单个参数方法的情况相同
public class VarargsExample{
public static void main( String args[] ){
test1();
test1(2);
}
public static void test1(int... x){
System.out.println("AssertionExample.test1(ARRAY METHOD)");
}
public static void test1(int x){
System.out.println("AssertionExample.test1(single param METHOD)");
}
public static void test1(){
System.out.println("AssertionExample.test1(PARAM LESS)");
}
}
First of call, the parameter-less overloading gets called because its signature is more specific than that of the overlauding with varargs. 首先,调用无参数重载,因为它的签名比使用varargs夸张的签名更具体。 It is in general a very bad idea to have two overloaded methods which perform a completely different operation.
通常,有两个执行完全不同的操作的重载方法是一个非常糟糕的主意。 So let's assume that the parameter-less method does the same thing as the varargs method when called without arguments, that is, the parameter-less method is a specialization of the varargs method.
因此,假设无参数调用时,无参数方法与varargs方法具有相同的功能,也就是说,无参数方法是varargs方法的特化。
Then a use-case is the following. 然后是一个用例。 Calling a varargs method always requires creating an array.
调用varargs方法始终需要创建一个数组。 Although, certainly at first, I wouldn't think about such minor optimizations too much, but it is an overhead which might, in some cases (for example in tight loop), be considerable enough.
虽然,起初我当然不会考虑这么小的优化,但是在某些情况下(例如在紧密循环中),这可能是一笔相当大的开销。 The parameter-less version of the method does not require creating an array, and additionally also may contain other optimizations for the specific case.
该方法的无参数版本不需要创建数组,并且还可以包含针对特定情况的其他优化。
Sometimes, one sees more than one specializations, one with no arguments, one with one, one with two, and a general method. 有时,一个人看到的专业不止一个,一个没有参数,一个带有一个,一个带有两个,以及一种通用方法。 For example:
例如:
void doSomething() { ... }
void doSomething(String a1) { ... }
void doSomething(String a1, String a2) { ... }
void doSomething(String... as) { ... }
But I suggest to only do this in a late stage of development, if at all. 但我建议仅在开发的后期才这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.