---
title: "Variable  Arguments(Varargs ) in Java"  
description: "If we don’t know how many arguments we will have to pass in the method .Varargs is the better approach."  
author: "Anupam Mishra"  
published: 2015-12-01  
updated: 2019-05-09  
canonical: https://www.mindstick.com/articles/11930/variable-arguments-varargs-in-java  
category: "java"  
tags: ["java", "j2ee"]  
reading_time: 2 minutes  

---

# Variable  Arguments(Varargs ) in Java

In Java, the [Variable](https://www.mindstick.com/blog/11493/what-is-a-variable) [Arguments](https://answers.mindstick.com/qa/92535/what-are-the-different-types-of-arguments) allows the method to accept zero or [multiple](https://www.mindstick.com/blog/12797/iowa-is-expected-to-see-heavy-growth-in-multiple-sectors) arguments. Before, variable arguments either we use overloaded method or take an array as the [method parameter](https://www.mindstick.com/forum/155764/explain-action-method-parameter).

But it was not considered good because it leads to the [maintenance](https://www.mindstick.com/articles/333912/maintenance-made-simple-how-online-tools-enhance-property-management) problem. [If we don](https://answers.mindstick.com/qa/33549/what-happens-if-we-don-t-drink-a-lot-of-water)’t know how many arguments we will have to pass in the method .Varargs is the better approach. The Varargs are widely used to that we don’t have to provide overloaded method.

##### Varargs syntax are as follows:

> ```
>        Return_type method_name (data_type …
> varable_name)      {        /* write method body */       }
> ```

##### Varargs are follows basically two rules. These are:

##### I. There can be only one variable arguments in the method.i.e.

> ```
> void Method1 (String … str, int …n)      //  compile time errorvoid Method1 (String str, int …n)      // successful compile & run
> ```

> II. Variable arguments (Varargs) must be the last arguments.i.e.
>
> ```
>   void Method1 (String … str, int n)   // compile time errorvoid Method1 (String str, int …n)    //successful compile & run
> ```

For example, we have working with various primitives types values in [methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) using Varargs.

> ```
> public class Varargs{static void var(int ... n){System.out.print("var(int ...) : "+"Number of
> arguments: "+n.length+"
> Contents ");for(int nn:n)System.out.print(nn+" ");System.out.println();}static void var(boolean ... b1){System.out.print("var(boolean ...) :
> "+"Number of arguments: "+b1.length+" Contents ");for(boolean b:b1)System.out.print(b+" ");System.out.println();}static void var(String str,int ... n){System.out.print("var(String str, int… n) :
> "+"Number of arguments: "+n.length+" Contents ");for(int n1:n)System.out.print(n1+" ");System.out.println();}public static void main(String[] args){var(12,3,3);var("Test",20,30);var(true,false,true);}}
> ```

> Output:
>
> Var(int …) : Number of arguments: 3 Contents 12 3 3
>
> Var([String](https://www.mindstick.com/interview/22908/what-are-java-string-class-method) str,int … n) : Number of arguments: 2 Contents 20 30
>
> Var([boolean](https://www.mindstick.com/forum/160332/how-does-the-boolean-function-work) …) : Number of arguments: 3 Contents true false true

\

---

Original Source: https://www.mindstick.com/articles/11930/variable-arguments-varargs-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
