Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

April 30, 2015 Categories: Core Java Basics. No Comments on Variable Length Arguments

In this article we will look at what is variable length arguments or varargs is and how to use variable length arguments in methods to solve the complexity of a Java program.


There might be some situations while creating methods, where the programmer might not know how many arguments are needed in the method definition or how many arguments are going to be passed at run-time by the user. Prior to Java 5, there were a couple of workarounds for this problem but was often complex or error prone.


With Java 5, a new feature called varargs was introduced which allows programmers to specify variable length arguments in the method definition. A method with variable length arguments is specified as shown below:


return-type method-name(type … variable-name) { }


In the above method declaration, ellipses (…) represents variable length arguments. We can also specify normal variables along with variable length arguments in the method definition. There are two rules that should be followed while working with variable length arguments. They are as follows:

  • When normal variables (arguments) are specified along with variable length arguments, the variable length argument should be the last in the list of arguments.
  • One cannot specify more than one variable length argument in a method definition.


Below example Java program demonstrates the use of variable length arguments (varargs):



Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up