Introduction To Varargs (Variable Arguments) In Java

Introduction

In this article we discuss Varargs (Variable Arguments) in Java.

Varargs (Variable Arguments)

Varargs allow the method to accept zero, one or more arguments. Before varargs either we used an array as the method parameter or use an overloaded method, but using this causes maintenance problems. If we are not sure how many arguments will be passed in the method, then varargs is the better approach.

Advantage

Using Varargs, we don't need additional overloaded methods so less coding is used.

How Varargs Works

When initializing the arguments, the compiler starts to match the argument list left-to-right with the given parameters. Once the given set of parameters are matched, then the remaining arguments are passed as an array to the method.

Syntax

It always uses ellipsis, in other words three dots (...) after the data type. The syntax is as follows:

return_type method_name(data_type... variable_Name) {}

Example

class VarargsEx1

  {

    static void print(String... values)

      {

        System.out.println("invoked print method");

      }

    public static void main(String args[])

      {

        print();//no/zero argument

        print("welcome", "to", "java", "world");//passed four arguments

      }

  }

Output

fig-1.jpg

Another Example

class VarargsEx2

  {

    static void print(String... values)

      {

        System.out.println("invoked print method");

        for(String str:values)

          {

            System.out.println(str);

          }

      }

    public static void main(String args[])

      {

        print();//no argument passed

        print("welcome");//passed only one argument

        print("welcome", "to", "java", "world");//passed four arguments

      }

  }

Output

fig-2.jpg

Rules for varargs

There are some rules for varargs that we must follow, otherwise the code can't compile. They are:

  1. Specify the type of the arguments (can be primitive or Object).
  2. Use the (…) syntax.
  3. You can have other arguments with varargs.
  4. There can be only one variable argument in the method.
  5. Variable arguments (varargs) must be the last argument.

A Third Example

The following example shows errors that cause the compiler to not run the program:

void method(String... str, int... b) {}// Compile time error

void method(int...a, String...str) {}// Compile time error

A Fourth Example

This example shows last the argument in the method.

class VarargsEx3

  {

    static void print(int n, String... values)

      {

        System.out.println("The no. is " +n);

        for(String str:values)

          {

            System.out.println(str);

          }

      }

    public static void main(String args[])

      {

        print(25, "hello");//only one argument

        print(50, "welcome", "to", "java", "world");//passed four arguments

      }

  }

Output

fig-3.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all