Hello team.
Today we will discuss what are variable length arguments in Java.
Let's start from the problem statement.
You want to create method, but you don't know how much arguments will be passed to that method.
There can be different business cases, but let's simplify and pretend that you need to create method
which will some numbers, but you don't know how much numbers will be passed to this method.
To not create method which returns sum of two numbers, and another method which returns sum of three
numbers, and so on, you create one method with variable length arguments.
The syntax is simple.
You need to specify type of arguments, right?
Ellipses and variable name.
Inside the method you can treat this variable as array.
This syntax will allow you to pass different amount of arguments to the method.
Here you can see that I pass five integers and here I pass just one argument.
In both cases this method is called and result is returned.
You can even change the way how you declare a main method instead of specifying that the main method
takes an array of strings, we can say that the main method can work with variable length arguments
of type string declaration like this is also possible.
Important thing to flag here is that variable length arguments should be declared as last parameter
in the method.
For example, this method declaration will produce compilation error because the variable argument type
int of the method must be the last parameter.
Because during runtime, JVM should know when int arguments are finished and strings are started, and
when we adjusted the method declaration, everything is fine and there is no compilation error.
Variable length argument always should be declared as the last parameter in the method.
You already saw real life example of variable length argument, but probably didn't pay enough attention.
Do you remember lesson about string formatting?
Method format takes variable length arguments as a parameter, because we never know how much format
specifiers will be present in string, which we need to format.
That's all what I wanted to share with you regarding variable length arguments.
Now let's recap what you have learned about methods in this section.
You learned how to declare and call methods.
You understand what method signature is and how we can overload methods.
Now you understand that both reference and primitive types of data in Java are passed by values.
During the method invocation, we wrote recursive methods and learn what variable length arguments are.
Let's take a look at your homework now.
Your homework primarily consists of coding exercises.
Now when you know how to write custom methods, how to use loops, arrays, and primitive types of data,
our tasks might become a little more complicated.
I tried to describe homework with as much details as I could.
Try to solve these tasks.
I will share with you source code of the solution for these tasks, but try to solve them by yourself
first.
Thanks a lot for your attention and see you in the next lesson.