i

JAVA Complete Course

Varargs

Since  JDK 5, Java introduced new feature which simplifies the creation of methods that need to take a variable number of arguments. It is called varargs which is short-form for variable-length arguments. A method which takes a variable number of arguments is a varargs method.

A varargs is specified by three dots(…). See an  example below:

A method together with varargs parameters can have other parameters as well, but there must exists only one varargs parameter that should be the last parameter in the method declaration.

Points to remember:

  • It is possible to overload varargs method but it may lead to ambiguity.

  • Before to JDK 5, varargs could be handled into two ways: One was using overloading, other was using array argument.

  • Method can have only one variable argument

  • Varargs must be the last argument.