Variable Length Arguments

Variable length arguments in short varargs was added in Java 1.5 and after reading about this concept I realized why the well known function of C, printf actually allows to pass variable arguments and other languages like C++,php etc. also supports this feature.

printf("Total %d wonders found in the %s", 5, "world");
printf("Insert natural number < %d ", 100);

varargs

varargs allows method to take arbitrary number of arguments. So that user can pass any number of argument he want while calling the method.

varargs is represented with three dots(periods) after declaring type of parameter which indicates that this parameter will be passed as sequence of arguments or indirectly as an array argument.

type...parameter

For example let’s say you want to call a method which adds int value to the stack but you want to add multiple values at a time but you are not sure how many values you can have during execution you can definitely make use of this feature.

When you call the method with arbitrary number of parameters it will be converted to array of specified type for you before the method call and pass the array as a parameter to the array.

NOTE: Here Arbitrary number of arguments in specific means 0 to Integer.MAX_VALUE number of arguments as that most size of array you can have.

Consider following example,

public void printVarArgs(int...numbers) {
        //HERE We have int[] numbers
	System.out.println("Total Numbers :"+ numbers.length);
	for(int i=0; i<numbers.length; i++) {
		System.out.println(numbers[i]);
	}
}

So following method calls stands valid to this method,

myClassObj.printVarArgs();//NOTE: Here it will pass empty array
myClassObj.printVarArgs(10,21,23);
myClassObj.printVarArgs(93);

Note here that,

varargs must be the last argument in the parameter list of method.

Compiler will not allow to declare method where varargs is not at last place in method parameter list.

Now let’s see why,

Consider  following method declaration,

public void printMyStrings(String...numbers, String s) {...}

Now when user call this method

obj.printMyStrings("10","Hello");

in this situations it becomes impossible for the compiler to handle this kind of situation. Although to avoid this kind of situation compiler will show following compile time error.

The variable argument type String of the method printVarArgs must be the last parameter

More over because of this feature following declaration of main method becomes valid,

public static void main(String...args)

More over note following statement about overriding varargs method

varargs methods should only override or be overridden by other varargs methods or method which takes array instead of varargs in the parameter list.

So method printVarArgs(int...numbers) can be overridden by printVarArgs(int...numbers) or printVarArgs(int[] numbers)

Although it’s a syntactic sugar you should avoid much use of varargs in your project as it has more overhead than passing array directly because of conversion of your parameters to an array before method call.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s