Switch to full style
General Java code examples
Post a reply

methods with variable arguments list

Sat Aug 22, 2009 4:15 pm

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




Re: methods with variable arguments list

Wed Nov 30, 2011 6:21 am

This program is awesome....

Post a reply
  Related Posts  to : methods with variable arguments list
 different between Local variable and Global variable     -  
 Which methods is best for SEO?     -  
 What Are Constructor Methods?     -  
 JSP Passing Arrays to Methods     -  
 Static Methods and Variables     -  
 methods in ActionListener interface     -  
 Calling Overloaded Methods     -  
 how to run inidividual test methods in junit4     -  
 Can we override static methods in java     -  
 Program to compute commission using two methods     -  

Topic Tags

Java Methods