• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java Varargs

March 13, 2014 //  by Rengasudharsan Srinivasan//  Leave a Comment

In this article, we will discuss about passing variable number of arguments to Java methods. Variable arguments feature was introduced in Java 1.5 and it is a very useful feature as we will see in the below examples.

Also Read:

  • Java 5 New Features
  • Java 7 Tutorials
  • Java 8 Tutorials

Prior to Java 1.5

Lets say we have an example as has been illustrated below. It is a very basic example having three different methods for the same functionality based on the number of inputs provided.

/**
 * This class is to illustrate the basic need for var arguments feature introduced in Java 1.5
 */
public class NoVarArgs {

     public static void main(String[] args) {

          /* Assume that you need to perform same operation but user can supply different number of parameters */

          int sub1 = subMethod1(30,10);
          int sub2 = subMethod2(30,20,10);
          int sub3 = subMethod3(30,20,3,2);

          System.out.println("Subtract method 1:" + sub1);
          System.out.println("Subtract method 2:" +  + sub2);
          System.out.println("Subtract method 3:" + sub3);
     }

     public static int subMethod1(int a, int b) {
          return a - b;
     }

     public static int subMethod2(int a, int b, int c) {
          return a - b - c;
     }

     public static int subMethod3(int a, int b,
                                    int c, int d) {
          return a - b - c - d;
     }

}

Can’t we do it better? Of course, there are alternatives. We could use an array shown as below:

/**
 * This class is to illustrate the basic need for var arguments feature  introduced in Java 1.5
 */

public class NoVarArgs {

     public static void main(String[] args) {

          int arr[] = new int[]{50, 10, 5, 3, 2};
          int num1 = 10;
          int num2 = 20;
          System.out.println(subVarArgsMethod(arr));
          System.out.println(subVarArgsMethod(num1, num2));

     }

     public static int subVarArgsMethod(int... a){
          int answer = 0;

          for (int i: a)
               answer = answer - i;

          return answer;
     }

}

or we could even use different data structures to do this. Wouldn’t it be nicer if Java has a feature to make it simpler in some way?. YES. Of course and that is where varargs feature introduced in Java 1.5 to helps us. Look at the below varargs example which passes the array of int values to a method.

/**
 * This class is to illustrate the basic need for var arguments feature  introduced in Java 1.5
 */

public class NoVarArgs {

     public static void main(String[] args) {

          int arr[] = new int[]{50, 10, 5, 3, 2};
          int num1 = 10;
          int num2 = 20;
          System.out.println(subVarArgsMethod(arr));
          System.out.println(subVarArgsMethod(num1, num2));

     }

     public static int subVarArgsMethod(int... a){
          int answer = 0;

          for (int i: a)
               answer = answer - i;

          return answer;
     }

}

How does Java do it? Java just creates an array internally allowing us to iterate through the variable number of arguments. The developer need not do anything extra to pass the values. Simple, isn’t it?

Varargs Syntax Advantages

  1. To use variable args features we just need to have 3 ellipsis dots like public static int subVarArgsMethod(int… a) in your method definition and that’s it. we can start using Java variable Arguments features now.
  2. Notice the advantage that variable arguments features bring in. In our earlier programs, we could pass only either an array or fixed number of arguments. But using variable args feature, we can see that we could use the same method for both arrays and any number of valid arguments.
  3. Of course, the same concepts can be used for Strings also.
  4. You can have several parameters in front of these variable arguments also. But you need to take care that this is the last argument in method definition. Eg.. public static int subVarArgsMet (double a, String s,int… a)

That’s it about Java variable arguments feature. Pass on your comments/correction if any. Thanks for reading.

This article is originally published at Java tutorials – Lets jump into the ocean, re-posted here with authors permission and as part of the JBC program.

Category: JavaTag: Core Java, Java 5.0

About Rengasudharsan Srinivasan

Renga is currently working as a software engineer. He has experience developing web applications using spring, struts and Hibernate framework. He is passionate about learning new technologies and has recently started writing a blog http://renga86.blogspot.com mainly focussing on Java.

Previous Post: « Java equals() and hashcode()
Next Post: Java Access Modifiers »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact