---
title: "Using VarArgs in Java"  
description: "What is Varargs in Java?Varargs (variable arguments) is a feature introduced in Java 1.5. It allows a method take an arbitrary number of values as arg"  
author: "Devesh Kumar Singh"  
published: 2015-11-06  
updated: 2018-03-13  
canonical: https://www.mindstick.com/blog/10981/using-varargs-in-java  
category: "java"  
tags: ["java"]  
reading_time: 2 minutes  

---

# Using VarArgs in Java

##### What is Varargs in Java?

#####

Varargs ([variable](https://www.mindstick.com/articles/1807/objective-c-data-types-variables-object-creation) [arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments)) is a [feature](https://www.mindstick.com/forum/541/what-is-new-feature-in-sql-server-2012) introduced in Java 1.5. It allows a method take an arbitrary number of [values](https://www.mindstick.com/forum/327/sum-textbox-values) as arguments.

##### How Varargs Work?

When varargs facility is used, it actually first creates an array whose size is the number of arguments passed at the call site, then puts the argument values into the array, and finally passes the array to the method.

##### When to Use Varargs?

As its [definition](https://yourviews.mindstick.com/view/70598/false-furore-over-the-leadership-definition-statement-of-army-chief-bipin-rawat) indicates, varargs is [useful](https://www.mindstick.com/articles/12694/how-dolomite-mineral-is-useful-in-human-life) when a method needs to deal with an arbitrary number of objects. One good example from Java SDK is [String.format](https://www.mindstick.com/forum/1763/string-format-with-null-values-c-sharp)(String format, Object... args). The string can format any number of [parameters](https://www.mindstick.com/articles/87/passing-parameters-in-c-sharp), so varargs is used.

##### Following are the two example using Varargs :

1) class sumvar

```
{    public static void main(String[] args)    {        int result1 = sum(1, 2);        int result2 = sum(1, 2, 3);        int result3 = sum(1, 2, 4, 5, 6, 1000);         System.out.println(result1 + "\n" + result2 + "\n" + result3);    }    public static int sum(int... args)    {        int add = 0;         if (args != null)        {            for (int val : args)            {                add += val;            }        }        return add;    }}
```

This will give the following output:

\
![Using VarArgs in Java](https://www.mindstick.com/blogs/d2a41131-65d7-49da-b19d-09772c284143/images/28fd0255-a5e8-44f8-85cc-676b815bf10d.png)

\
2) public class var

```
 {    public static void main(String args[])   {      var obj= new var();      String sum="";      sum = obj.add(args);      System.out.println("The sum of the numbers is: " + sum);   }    public static String add(String... args)   {      int sum, i;      sum=0;      for(i=0; i< args.length; i++)    {         sum += Integer.parseInt(args[i]);      }            String s= Integer.toString(sum);      return(s);      }}
```

This will give the following output:

![Using VarArgs in Java](https://www.mindstick.com/blogs/d2a41131-65d7-49da-b19d-09772c284143/images/fdff1a25-7e55-4023-860c-770b7bdb0b83.png)

The above two examples are different in the way that [one of them](https://answers.mindstick.com/qa/47593/what-are-the-prevalent-water-proofing-systems-write-a-short-notes-on-any-one-of-them) passes values as the argument and other ask user to input value at run time.

---

Original Source: https://www.mindstick.com/blog/10981/using-varargs-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
