177

Is it possible to declare a method that will allow a variable number of parameters ?

What is the symbolism used in the definition that indicate that the method should allow a variable number of parameters?

Answer: varargs

user69514
  • 26,935
  • 59
  • 154
  • 188
  • 13
    Since its homework, we don't want to know your question, we just want to know you are learning. – Dave Nov 18 '13 at 17:19

6 Answers6

313

That's correct. You can find more about it in the Oracle guide on varargs.

Here's an example:

void foo(String... args) {
    for (String arg : args) {
        System.out.println(arg);
    }
}

which can be called as

foo("foo"); // Single arg.
foo("foo", "bar"); // Multiple args.
foo("foo", "bar", "lol"); // Don't matter how many!
foo(new String[] { "foo", "bar" }); // Arrays are also accepted.
foo(); // And even no args.
ErikE
  • 48,881
  • 23
  • 151
  • 196
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 2
    Is it possible to do various type of paramteres? e.g. (String...strs, int... ints). What about just any type of argument in any order? – trusktr Oct 04 '13 at 01:49
  • 6
    @trusktr: if you want any object, just use `Object...`. – BalusC Oct 04 '13 at 01:51
  • 3
    @trusktr No, primitives are not objects. There is a great explanation of the difference here: http://www.programmerinterview.com/index.php/java-questions/difference-between-a-primitive-type-and-a-class-type/ – Dick Lucas Aug 03 '14 at 15:25
  • 6
    @Richard: Using `Object... args` will work with primitives because of [autoboxing](https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html). – Sumit May 02 '16 at 15:37
  • How would you check the number of arguments? – Stevoisiak Apr 30 '17 at 18:11
  • 1
    @StevenVascellaro : I assume that the arguments are handled like an array, so you could probably simply do `varargs.length` – Luatic May 25 '17 at 14:23
18

Yes, it's possible:

public void myMethod(int... numbers) { /* your code */ }
Skaldebane
  • 95
  • 2
  • 11
Dolph
  • 49,714
  • 13
  • 63
  • 88
17
Variable number of arguments

It is possible to pass a variable number of arguments to a method. However, there are some restrictions:

  • The variable number of parameters must all be the same type
  • They are treated as an array within the method
  • They must be the last parameter of the method

To understand these restrictions, consider the method, in the following code snippet, used to return the largest integer in a list of integers:

private static int largest(int... numbers) {
     int currentLargest = numbers[0];
     for (int number : numbers) {
        if (number > currentLargest) {
            currentLargest = number;
        }
     }
     return currentLargest;
}

source Oracle Certified Associate Java SE 7 Programmer Study Guide 2012

Victor Sergienko
  • 13,115
  • 3
  • 57
  • 91
Abey Ella
  • 171
  • 1
  • 2
14

For different types of arguments, there is 3-dots :

public void foo(Object... x) {
    String myVar1  = x.length > 0 ? (String)x[0]  : "Hello";
    int myVar2     = x.length > 1 ? Integer.parseInt((String) x[1]) : 888;
} 

Then call it

foo("Hii"); 
foo("Hii", 146); 

for security, use like this:
if (!(x[0] instanceof String)) { throw new IllegalArgumentException("..."); }

The main drawback of this approach is that if optional parameters are of different types you lose static type checking. Please, see more variations .

T.Todua
  • 53,146
  • 19
  • 236
  • 237
6

Yup...since Java 5: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

Roland Bouman
  • 31,125
  • 6
  • 66
  • 67
0

Yes Java allows vargs in method parameter .

public class  Varargs
{
   public int add(int... numbers)
   { 
      int result = 1; 
      for(int number: numbers)
      {
         result= result+number;  
      }  return result; 
   }
}
Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
Code_Eat_Sleep
  • 303
  • 2
  • 6