From the course: Java Essential Training: Syntax and Structure

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Variable arguments

Variable arguments

- [Instructor] Just like objects and variables, you can also pass arrays into methods and have methods return them. In fact, you've seen this before with the main method. When a method explicitly specifies an array must be passed in, it requires the calling method to first create an array to send in. However, notice there is no length specified in the parameter. So even if the caller wanted to send only one value, they still need to put it inside of an array. Variable arguments, also often referred to as varargs, allows you to be a bit more flexible with the arguments that you send in. For example, let's say we wanted to create a method that calculates the sum for any amount of integers. You may want to send two numbers, or three, or four, or 100. We could do so by specifying the data type of the parameter as always, but then using three dots and then a variable name. This is a variable argument, and it means that you…

Contents