Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Core Java Basics. No Comments on Variable Length Arguments
0
(0)

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) { }


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

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):

class VarArgsDemo
{
   public static void main(String[] args)
   {
      VarArgsDemo v = new VarArgsDemo();
      v.sum(1);
      v.sum(1, 2);
      v.sum(1, 2, 3);
   }
   public void sum(int ... var)
   {
      int s = 0;
      for(int x : var)
         s += x;
      System.out.println("Sum is: " + s);
   }
}

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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 *