Variable arguments or varargs methods in java

Java variable arguments was introduced in java 5 and it allows methods to take as many number of arguments you want.


We use three dots (…) also known as ellipsis to define variable arguments.

There are some points which you need to keep in mind while using varargs methods:

  1. You can have only one variable arguments (ellipsis) in one method
  2. varargs should be last parameter in the method, otherwise method won’t compile.

Why to use variable arguments or varargs methods:

There are two alternatives which can be used which developers used before java 1.5

  1. Method overloading
  2. Passing array to the method

Method overloading may not be good idea because you don’t know number of methods you need to overload.You can pass arguments in the array but why don’t we can directly use varargs method and let compiler create array for us.

How varargs works internally.

While invoking varargs method, compiler matches argument from left to right, Once fixed number of arguments are matched, rest all arguments are created as array and passed to method.
For example:

Lets say you have method signature as below:

and you invoke this method as

then “test” and 1 will be passed as normal parameters and 2,3 and 4 will be passed as array.

Java variable arguments or varargs example:

When you run above program, you will get below output:

Was this post helpful?

Related Posts

  • 30 January

    Difference between early binding and late binding in java

    Table of ContentsEarly Binding or Static BindingWhy are static final and private methods associated by Early Binding?Code Example:Analysis:Late Binding or Dynamic BindingCode Example:Output:Analysis:Differences between Static and Dynamic BindingWas this post helpful? Binding in Java refers to the process of associating a method or function body with a method or function call by the Java Compiler. […]

  • 04 November

    Can we overload main method in java

    Table of ContentsOverload main method in javaWas this post helpful? In this post, we will see about "Can we overload main method in java".This is one of the most asked Core java interview questions. Yes, we can overload main method in java but when you run your program, JVM will search for public static void […]

  • 02 November

    Dynamic method dispatch in java

    Table of ContentsDynamic Method DispatchWas this post helpful? In this post, we will about Dynamic method dispatch which is also referred as run time polymorphism. Dynamic Method Dispatch Dynamic method dispatch is a technique by which call to a overridden method is resolved at runtime, rather than compile time.When an overridden method is called by […]

  • 29 October

    Can we override static method in java

    No, we can not override static method in java. Static methods are those which can be called without creating object of class,they are class level methods. On other hand,If subclass is having same method signature as base class then it is known as method overriding. Its execution decided at run time. Below are the reasons why […]

  • 26 August

    Copy Constructor in java

    Table of ContentsJava Copy ContructorWas this post helpful? In this post, we will see about copy constructor in java. Copy constructor is the constructor which takes parameter as object of same class and copy each field of the class to the new object. Unlike C++, java does not provide default copy contructor. You need to […]

  • 26 August

    Java default constructor

    Table of ContentsIs there any difference between no argument constructor and default constructor?Was this post helpful? In this post, we will see about Java default constructor. Default constructor is the no arg constructor which is inserted by compiler unless you provide any other constructor explicitly. You won’t able to see it as it is present […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.