Tutorial Java SCJP – #14.2 How to define methods with variable argument lists – var-args

Starting with Java 5.0 you can define methods with variable argument lists (var-args).  These methods can be used in situations when you don’t know the exact number of arguments. These is also available in other programing languages and even I is not intensively used, it is a topic for the SCJP exam.

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.

In order to understand var-args methods, let’s think that we want to implement a method used to add 2 numbers. The method looks like this:

public class Main {
    public static void main(String[] args) {
        int v1 = 10, v2 = 20;
        System.out.println("The sum is "+doAdd(v1, v2));
    }
    public static int doAdd(int vb1, int vb2)
    {
        return vb1+vb2;
    }
}

If the situations changes and I want a method for adding 3 numbers, I will define it like this

public class Main {
    public static void main(String[] args) {
        int v1 = 10, v2 = 20, v3 = 30;
        System.out.println("The sum is "+doAdd3(v1, v2, v3));
    }
    public static int doAdd3(int vb1, int vb2, int vb3)
    {
        return vb1+vb2+vb3;
    }
}

And this scenario can continue. So, for efficiency, we need a method that can add any number of values – a method with variable argument lists.

The syntax for defining a var-args method is

[access modifiers][non-access modifiers] return_type functionName(type param1, type param2, type … parametersList)

where:

param1 / param2 – input parameters for the method; these are optional;

parametersList – the array that stores the variable argument list; all the arguments have the same type (primitive or reference); the var-args parameter is declared at the end;

Using a var-args method, the previous problem has one solution:

    public static int doAdd(int ... values)
    {
        int sum = 0;
        for(int i=0;i < values.length;i++)
            sum+=values[i];
        return sum;
    }

and we can use the method like this:

public class Main {
    public static void main(String[] args) {
        int v1 = 10, v2 = 20, v3 = 30;
        System.out.println("The sum of first 2 values is "+doAdd(v1, v2));
        System.out.println("The sum of all values is "+doAdd(v1, v2, v3));
        System.out.println("The sum of none of the values "+doAdd());
    }
}

The var-args method can have other input parameters, but they must be defined before the var-arg parameter. The next method, determines the sum of positive/negative values:

    public static int doConditionalAdd(boolean flag, int ... values)
    {
        int sum = 0;
        for(int i=0;i < values.length;i++)
        {
            if(flag && values[i] >= 0)
                sum+=values[i];
            if(!flag && values[i] < 0)
                sum+=values[i];
        }
        return sum;
    }

An alternative for var-args methods is to use arrays as input parameters. The difference, is that you must define an array and you must copy the variables values before calling the method:

public class Main {
    public static void main(String[] args) {
        int v1 = 10, v2 = 20, v3 = 30;
        int[] array = new int[3];
        array[0] = v1;
        array[1] = v2;
        array[2] = v3;
	//an alternative is int[] array = {v1,v2,v3};
        System.out.println("The sum of all values is "+doAdd(array));
    }
    public static int doAdd(int[] values)
    {
        int sum = 0;
        for(int i=0;i < values.length;i++)
            sum+=values[i];
        return sum;
    }
}

Important facts about var-args methods for SCJP:

  • all the arguments from the var-arg list have the same type (primitive or reference);
  • the method can have other paramaters;
  • a method can have only one var-arg list;
  • the var-arg list must be the last parameter in the method signature;

Based on previous rules, the next declarations are illegal:

    //wrong syntax
    public static int doAdd(int values ... )
    {   }
    //var-arg is not the last parameter
    public static int doAdd(int... values, boolean flag )
    {   }
    //multiple var-arg parameters
    public static int doAdd(int... values1, int... values2 )
    {   }

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.