[问题记录]Java关于可变参数重载问题的测试

突然发现java的可变参数重载是存在歧义的,只是不知道java内部是怎么处理的,特意做如下实验:

测试代码如下:

 /**
* Created by ieayoio on 16-3-1.
*/
public class VarTest { public static void aaa(int ...a){
System.out.println("可变参数重载");
} public static void aaa(int a){
System.out.println("非可变参数重载");
}
public static void main(String[] args) { }
}

1.在14行添加aaa();时运行结果为:

可变参数重载

Process finished with exit code 0

2.在14行添加aaa(111,111);时运行结果为:

可变参数重载

Process finished with exit code 0

3.在14行添加aaa(111);时运行结果为:

非可变参数重载

Process finished with exit code 0

4.将代码改为以下:

 /**
* Created by ieayoio on 16-3-1.
*/
public class VarTest { public static void aaa(int a){
System.out.println("非可变参数重载");
} public static void aaa(int ...a){
System.out.println("可变参数重载");
} public static void main(String[] args) {
aaa(111);
}
}

结果依然为:

非可变参数重载

Process finished with exit code 0

结论:当使用可变参数重载方法时,java内部会优先使用非可变参数方法去执行程序

原文链接:http://www.cnblogs.com/ieayoio/p/5231751.html

上一篇:Java中可变参数


下一篇:c 中可变参数的实现