Can we call same method with different numbers of parameter?
class VarArgMethod1
{
public static void m1(int...x)
{
System.out.println("var-arg method");
}
public static void main(String[] args)
{
m1();
m1(10);
m1(10,20);
}
}
/*
Output:- var-arg method
var-arg method
var-arg method
Explanation:- The above program is an example of variable argument method. Until 1.4 version we cannot declare a method with variable number of arguments. If there is a change in number of arguments in a method then compulsary we had to declare a new method which increases length of the code which reduces readability. Hence to overcome this problem SUN people introduced var-arg method concept in jdk 1.5 version. Using this concept we can call the method by passing variable number of arguments even without argument also.
Note:- 3 dots is called Ellipsis.
*/
{
public static void m1(int...x)
{
System.out.println("var-arg method");
}
public static void main(String[] args)
{
m1();
m1(10);
m1(10,20);
}
}
/*
Output:- var-arg method
var-arg method
var-arg method
Explanation:- The above program is an example of variable argument method. Until 1.4 version we cannot declare a method with variable number of arguments. If there is a change in number of arguments in a method then compulsary we had to declare a new method which increases length of the code which reduces readability. Hence to overcome this problem SUN people introduced var-arg method concept in jdk 1.5 version. Using this concept we can call the method by passing variable number of arguments even without argument also.
Note:- 3 dots is called Ellipsis.
*/
0 comments: