Tuesday, 30 August 2016

Var-arg method

Var_arg Method:
In java applications, if we declare any method with “n” number of parameters. Then we are able to access that method by passing the same “n” number of parameter values, it is not possible to  access that method by passing n+1 number of parameter value and n-1 number of parameter values.
In java applications, as per the requirement, if we want to access a method by passing variable number of parameters then we have to use “Variable_Arguments” method, in short, “Var_Arg method”.
Var_Arg method is a java method provided by JDK 5.0 version, it contains Var_arg parameter.
Syntax of Var_Arg parameters     
Data_Type ... var_Name
Syntax of Var_Arg method

void m2(int ... a)
{
                ---implementation---
}    
Note:-
When we access var_Arg method with “n” number of parameter values then var_args parameter will be converted to an array of the same data type and all the “n” number of parameter value are stored in the converted array.
Ex code :-
class A
{
                void add(int ... a) //int[] a
                {
                                System.out.println("No Of Arguments :"+a.length);
                                System.out.println("Argument List:");
                                int result=0;
                                for(int i=0; i<a.length;i++)
                                {
                                                System.out.println(a[i]+" ");
                                                result=result+a[i];
                                }
                                System.out.println();
                                System.out.println("Addition  :"+result);
                                System.out.println("-------------------");
                }
}
class Test
{
                public static void main(String[] args)
                {
                                A a=new A();
                                a.add();
                                a.add(10);
                                a.add(10,20);
                                a.add(10,20,30);
                }
}
Output:-
No Of Arguments :0
Argument List:

Addition  :0
-----------------
No Of Arguments :1
Argument List:
10

Addition  :10
-----------------
No Of Arguments :2
Argument List:
10
20

Addition  :30
-----------------
No Of Arguments :3
Argument List:
10
20
30

Addition  :60
-----------------
Note:-
In java applications, we are able to provide normal parameter along with Var_Args parameters in var_args method, but, they must not be provide after var_arg parameter, they must be provide before var_args parameter.
Q) Is it possible to provide more than one var_var parameter with in asingle var_Arg method ?
Ans):-
No, it is not possible to provide more then one var_Arg parameter within a single var_Args method, because, var_Arg parameter must be last parameter in Var_Arg method.
1. void m1(int ... i, float f){}-->Invalid
2. void m1(float f, int ... i){}-->Valid
3. void m1(float ... f, int ... i){}-->Invalid

Date july-21-2016


No comments:

Post a Comment