The following code in Java uses varargs to overload methods.
final public class Main
{
private void show(int []a)
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+"\t");
}
}
private void show(Object...a)
{
for(int i=0;i<a.length;i++)
{
System.out.print(a[i]+"\t");
}
System.out.println("\nvarargs called");
}
public static void main(String... args)
{
int[]temp=new int[]{1,2,3,4};
Main main=new Main();
main.show(temp);
main.show(); //<-- How is this possible?
}
}
The output of the above code is as follows.
1 2 3 4
varargs called
The last line main.show();
within the main()
method invokes the show(Object...a){}
method which has a varargs formal parameter. How can this statement main.show();
invoke that method which has no actual arguments?