Java varargs

0

Before Java 5.0, if you want to print an array of int, you need to pass an array of int to print(int[] ids)

    private void print(int[] ids) {
        for (int id : ids) {
            System.out.println(id);
        }
    }

And this is how we used to call:

    public void testPrintIntArray() {
        print(new int[]{1, 2, 3});
    }

If you notice the array argument new int[]{1, 2, 3} that we pass to print is a bit verbose. You need to create an instance of int array, open curly braces, type-in comma separated int values and close curly braces. varargs feature automates and hides the process of array creation. You only need to pass the comma separated values.

This feature is also called “variable-length argument lists,” “variable arguments,” “var-args,” “varargs,” or “variable arity parameter.”

To declare vararg parameter, you must first specify the type of the argument and the follow the type with an ellipsis (…), a space (optional) and the name of the array that will hold the parameter received.

The three periods after the parameter’s type indicate that the argument may be passed as an array or as a sequence of arguments.

        private void print(int...ids) {
        System.out.println("is ids null? ");
        System.out.println(ids == null);
        if (ids != null) {
            System.out.println("ids length: " + ids.length);
        }
        for (int id : ids) {
            System.out.println(id);
        }
    }

To call print, you just need to pass the int values:

public void testVarArgs() {
    print(1, 2, 3, 4);
    print(new int[]{1, 2, 3});
}

It is backward compatible so you can even pass an array and it will still work.

Lets see what happens if we pass call print() without any arguments.

public void testVarArgs() {
    print();
}

Test passes. Output:

is ids null? 
false
ids length: 0

It seems java internally creates an empty array and passes it to print().

Passing additional arguments

It is legal to pass other parameters in a method that uses a var-args, provide varargs is the last parameter in the method signature.

Why is varargs always the last parameter in a method signature?

An example will make it clear:
Suppose, I want to pass an additional int parameter called myInt as the final parameter along with the varargs:

    private void print(int...ids, int myid) {
        System.out.println("MyId: " + myid);
        for (int id : ids) {
            System.out.println(id);
        }
    }

Compiler throws error:
The variable argument type Integer of the method print must be the last parameter
If we call print(1, 2, 3, 4), there will some obvious ambiguity. Is varargs ids going to have all the four elements 1, 2, 3, 4 or first three elements 1, 2, 3.

Lets now fix the above method so that ids become the last argument.

    private void print(int myid, int...ids) {
        System.out.println("MyId: " + myid);
        for (int id : ids) {
            System.out.println(id);
        }
    }

Now compiler’s job is easy, in the call to print(1, 2, 3, 4), it assigns 1 to myid and uses rest of the arguments to create an array of three elements 2, 3, 4.

Since varargs will always have to be the final argument you cannot pass more than one varargs to a method so below will be illegal.

    private void print(int...ids1, int...ids2) {
    }

About Author

Ram's expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring. You can reach him at [email protected]

Leave A Reply