Methods with Variable Arguments

We write a method in the form of sum(int a, int b), since the method has only two parameters, so we can pass only two values or two arguments to this method, it is not possible to pass more than two values to this method sum()

Suppose we want to find sum of three numbers, then to pass three values we need to write another method having three parameters like

sum(int a, int b,int c) , Similarly if we have to find the sum of four numbers, again we need to create another method having four parameters like this

sum(int a, int b,int c,int d) is needed.

The problem is we need to write a new method every time when we change the number of arguments.

On the other hands think if the same method can accept any number of arguments, then the method will be very useful for us, observe the

following methods:

int sum(int a, int b)

int sum(int a, int b, int c)

int sum(int a, int b, int c, int d)

instead of writing the above methods we can write a single method like this

int sum(int x[]) → This method can accept a group of integer and find their sum but there is a restriction while using this method when we call this method we must pass an array, we cannot pass individual values for example calling this method like sum(10,20) -> it will throw error

So here comes the Methods with Variable Arguments, hence there is a solution for this problems, We can write : int sum(int … x)

Please observe the three dots between the datatype and the variable.

These three dots represent that it is not an ordinary parameter but a parameter that can accept any number of arguments. This type of method is called Method with Variable Arguments or simply method with varargs

So a method with a variable argument is a method that can accept zero or more number of arguments, we can pass individual elements or an array also to this method.

So we need to follow some rules while writing method with Variable Arguments in our code. → These are given below

(1) We should write the method name followed by simple braces() and in the braces we can define the datatype and then three dots and then the variable or objects, for example → int sum(int … x)

(2) We can also define other arguments along with varargs, in that case the varargs should be the last in the method parameters

example → int sum(int a, int b, int … x)

here a, b represents ordinary arguments and x represents the vararg.

(3) We can use only one variable argument in a method,

(4) Internally vararg is treated as 1D array for example if we pass individual elements to sum() method, the vararg x will become an array and stores them all, suppose if we pass an array to this method then all the elements of the array are copied into x array.

So at last, its time to apply all the theories by writing a program. So lets write a program on method with Variable Arguments.

package com.methodsvarargs;

public class MethodsWithVarArgs {

public static int max(int… x) {

int max = x[0];

for (int i = 1; i < x.length; i++) {

if (max < x[i])

max = x[i];

}

return max;

}

public static void main(String[] args) {

int[] arr1 = { 40, 15, 5, 45, 90 };

int res = max(arr1);

System.out.println(“Maximum element in the Array is : “ + res);

int arr2[] = { 1, 2, 3 };

int res2 = max(arr2);

System.out.println(“Maximum element in the Array is : “ + res2);

int res3 = max(100, 200);

System.out.println(“Maximum element in the Array is : “ + res3);

}

}

And guess the output:

Maximum element in the Array is : 90

Maximum element in the Array is : 3

Maximum element in the Array is : 200

So thats all in this post..Keep Learning.. see you in the next post.

Engineer | Blogger | Traveller