Varargs (Variable Argument)

Download java eBook

Remarks

A “varargs” method argument allows callers of that method to specify multiple arguments of the designated type, each as a separate argument. It is specified in the method declaration by three ASCII periods (...) after the base type.

The method itself receives those arguments as a single array, whose element type is the type of the varargs argument. The array is created automatically (though callers are still permitted to pass an explicit array instead of passing multiple values as separate method arguments).

Rules for varargs:

  1. Varargs must be the last argument.
  2. There can be only one Varargs in the method.

You must follow above rules otherwise program will give compile error.

Related Examples

Stats

154 Contributors: 7
Friday, March 24, 2017
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook