Code subject: methods with variable arguments list
Posted: Sat Aug 22, 2009 4:15 pm
Joined: Tue Mar 27, 2007 10:55 pm Posts: 2103 Location: Earth Has thanked: 39 time Have thanks: 57 time
Methods with Variable Arguments List :
With java 5, you can have a function with variable number of arguments (called for short: var-args) , you do this following the rules below : 1.Specify the type of arguments (can be primitive or Object). 2.Use the (…) syntax. 3. You can have other arguments with var-arg . 4.You can have only one var-arg . 5.The var-arg must be last on in method arguments.
See the following example :
Code:
public class VariableArg { public static void main(String args[]) { int output = sum(1,2,4,5,1,3); System.out.println("Sum = "+output); }
public static int sum(int ... x) { int result =0 ; for (int i=0;i<x.length;i++) { result+=x[i]; } return result; }
}
The output is :
Code:
Sum = 16
_________________ Currenlty programming with : java , html , php , and javascript . (OCJP-6 certified )
pulipatiudaykiran
Code subject: Re: methods with variable arguments list