varargs in java

The varargs (variable arguments) introduced in jdk1.5, where we can define a method which can take multiple numbers of parameters. In case of varargs we know need to write overloaded methods. We define a single method which can take n numbers of arguments.

Here we will see –

  1. What is the syntax to define varargs and what is the benefit of varargs?
  2. Valid and invalid syntaxes for varargs.
  3. Use of varargs in real time project.

What is the syntax to define varargs and what is the benefit of varargs?

Syntax –

public void m1(int... a){
		
	}

Let’s see an example of varargs-

public class VarArgsExample {

	public void m1(int... a) {
		System.out.println("this method can take any number of parameters");
	}

	public static void main(String[] args) {

		VarArgsExample argsExample = new VarArgsExample();
		argsExample.m1(10, 20, 5, 6, 30);// we can n number of parameters
	}
}

Output is – this method can take any number of parameters

In the above program, we have defined method m1() which can n number of arguments. Let’s see what is the problem without varargs. Suppose we have a requirement to add some number. We can have two number or three or five or ten.

Program without varargs –

public class VarArgsExample {

	public void sumOfNumbers(int a, int b) {
		System.out.println("sum of two numbers  " + (a + b));
	}

	public void sumOfNumbers(int a, int b, int c) {
		System.out.println("sum of three numbers  " + (a + b + c));
	}

	public void sumOfNumbers(int a, int b, int c, int d) {
		System.out.println("sum of four numbers  " + (a + b + c + d));
	}

	public static void main(String[] args) {

		VarArgsExample argsExample = new VarArgsExample();
		argsExample.sumOfNumbers(10, 20);
		argsExample.sumOfNumbers(10, 20, 30);
		argsExample.sumOfNumbers(10, 20, 30, 40);

	}
}

Output is –

sum of two numbers 30
sum of three numbers 60
sum of four numbers 100

Program with varargs –

public class VarArgsExample {

	public void sumOfNumbers(int... a) {

		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum = sum + a[i];
		}
		System.out.println("sum of " + a.length + " numbers is " + sum);

	}

	public static void main(String[] args) {

		VarArgsExample argsExample = new VarArgsExample();
		argsExample.sumOfNumbers(10, 20);
		argsExample.sumOfNumbers(10, 20, 30);
		argsExample.sumOfNumbers(10, 20, 30, 40);

	}
}

Output is –

sum of 2 is 30
sum of 3 is 60
sum of 4 is 100

In above example we are performing the same operation in a single method(Just for example, In real time, the scenario may be different, we will see later). We no need to define multiple overloaded version of sumOfNumbers() method.

Benefit of varargs – 

We can reduce a lot of code using var args.

Valid and invalid syntaxes for varargs.

Valid declaration of varargs – 

public void validDeclartion1(int... a) {
//this is a valid declaration of var args
}

public void validDeclartion1(int ...a) {
//this is a valid declaration of var args
}

public void validDeclartion2(int...a) {
//this is a valid declaration of var args
}

public void validDeclartion3(int a, String... s) {
//varargs must be last parameter
}


Invalid declarations of varargs – 

public void validDeclartion2(int... a, String s) {
//varargs must be in last, if we have multiple parameter 
}

 

Use of varargs in real time project.

Real time scenario 1 – 

Suppose you want to create a composite key in hibernate on basis of two attributes in a table. We can define a method something called createPrimaryKey(Object… columName ), you can pass multiple ids which can be used to create a composite key.

Object createPrimaryKey(Object... columName )

Real time scenario 2 – 

Suppose you have a requirement, where we have to define a constructor which can take multiple numbers of list of Stringstrings. One option is we can define multiple overloaded version of the constructor or we can use varargs as a parameter in constructor. Let’s see an example –

import java.util.Arrays;
import java.util.List;

public class VarArgsExample {
	private List<List<String>> listOfString;

	public VarArgsExample(List<String>... listOfString) {
		this.listOfString = Arrays.asList(listOfString);
		System.out.println("This is just a dummy example " + listOfString);
	}

	public static void main(String[] args) {
		VarArgsExample argsExample = new VarArgsExample();
	}
}

Output is – This is just a dummy example [Ljava.util.List;@15db9742