| 1 |
| 00:00:00,000 --> 00:00:00,000 |
| Hello team. |
|
|
| 2 |
| 00:00:00,000 --> 00:00:04,000 |
| Today we will discuss what are variable length arguments in Java. |
|
|
| 3 |
| 00:00:04,000 --> 00:00:06,000 |
| Let's start from the problem statement. |
|
|
| 4 |
| 00:00:06,000 --> 00:00:11,000 |
| You want to create method, but you don't know how much arguments will be passed to that method. |
|
|
| 5 |
| 00:00:11,000 --> 00:00:16,000 |
| There can be different business cases, but let's simplify and pretend that you need to create method |
|
|
| 6 |
| 00:00:16,000 --> 00:00:22,000 |
| which will some numbers, but you don't know how much numbers will be passed to this method. |
|
|
| 7 |
| 00:00:22,000 --> 00:00:27,000 |
| To not create method which returns sum of two numbers, and another method which returns sum of three |
|
|
| 8 |
| 00:00:27,000 --> 00:00:31,000 |
| numbers, and so on, you create one method with variable length arguments. |
|
|
| 9 |
| 00:00:31,000 --> 00:00:33,000 |
| The syntax is simple. |
|
|
| 10 |
| 00:00:33,000 --> 00:00:36,000 |
| You need to specify type of arguments, right? |
|
|
| 11 |
| 00:00:36,000 --> 00:00:38,000 |
| Ellipses and variable name. |
|
|
| 12 |
| 00:00:38,000 --> 00:00:41,000 |
| Inside the method you can treat this variable as array. |
|
|
| 13 |
| 00:00:42,000 --> 00:00:46,000 |
| This syntax will allow you to pass different amount of arguments to the method. |
|
|
| 14 |
| 00:00:46,000 --> 00:00:51,000 |
| Here you can see that I pass five integers and here I pass just one argument. |
|
|
| 15 |
| 00:00:51,000 --> 00:00:55,000 |
| In both cases this method is called and result is returned. |
|
|
| 16 |
| 00:00:55,000 --> 00:01:00,000 |
| You can even change the way how you declare a main method instead of specifying that the main method |
|
|
| 17 |
| 00:01:00,000 --> 00:01:06,000 |
| takes an array of strings, we can say that the main method can work with variable length arguments |
|
|
| 18 |
| 00:01:06,000 --> 00:01:10,000 |
| of type string declaration like this is also possible. |
|
|
| 19 |
| 00:01:10,000 --> 00:01:15,000 |
| Important thing to flag here is that variable length arguments should be declared as last parameter |
|
|
| 20 |
| 00:01:15,000 --> 00:01:16,000 |
| in the method. |
|
|
| 21 |
| 00:01:17,000 --> 00:01:22,000 |
| For example, this method declaration will produce compilation error because the variable argument type |
|
|
| 22 |
| 00:01:22,000 --> 00:01:25,000 |
| int of the method must be the last parameter. |
|
|
| 23 |
| 00:01:25,000 --> 00:01:31,000 |
| Because during runtime, JVM should know when int arguments are finished and strings are started, and |
|
|
| 24 |
| 00:01:31,000 --> 00:01:36,000 |
| when we adjusted the method declaration, everything is fine and there is no compilation error. |
|
|
| 25 |
| 00:01:37,000 --> 00:01:41,000 |
| Variable length argument always should be declared as the last parameter in the method. |
|
|
| 26 |
| 00:01:41,000 --> 00:01:47,000 |
| You already saw real life example of variable length argument, but probably didn't pay enough attention. |
|
|
| 27 |
| 00:01:47,000 --> 00:01:50,000 |
| Do you remember lesson about string formatting? |
|
|
| 28 |
| 00:01:50,000 --> 00:01:56,000 |
| Method format takes variable length arguments as a parameter, because we never know how much format |
|
|
| 29 |
| 00:01:56,000 --> 00:01:59,000 |
| specifiers will be present in string, which we need to format. |
|
|
| 30 |
| 00:02:00,000 --> 00:02:04,000 |
| That's all what I wanted to share with you regarding variable length arguments. |
|
|
| 31 |
| 00:02:04,000 --> 00:02:08,000 |
| Now let's recap what you have learned about methods in this section. |
|
|
| 32 |
| 00:02:08,000 --> 00:02:10,000 |
| You learned how to declare and call methods. |
|
|
| 33 |
| 00:02:10,000 --> 00:02:14,000 |
| You understand what method signature is and how we can overload methods. |
|
|
| 34 |
| 00:02:14,000 --> 00:02:20,000 |
| Now you understand that both reference and primitive types of data in Java are passed by values. |
|
|
| 35 |
| 00:02:20,000 --> 00:02:26,000 |
| During the method invocation, we wrote recursive methods and learn what variable length arguments are. |
|
|
| 36 |
| 00:02:26,000 --> 00:02:28,000 |
| Let's take a look at your homework now. |
|
|
| 37 |
| 00:02:29,000 --> 00:02:32,000 |
| Your homework primarily consists of coding exercises. |
|
|
| 38 |
| 00:02:32,000 --> 00:02:38,000 |
| Now when you know how to write custom methods, how to use loops, arrays, and primitive types of data, |
|
|
| 39 |
| 00:02:38,000 --> 00:02:41,000 |
| our tasks might become a little more complicated. |
|
|
| 40 |
| 00:02:41,000 --> 00:02:45,000 |
| I tried to describe homework with as much details as I could. |
|
|
| 41 |
| 00:02:45,000 --> 00:02:47,000 |
| Try to solve these tasks. |
|
|
| 42 |
| 00:02:47,000 --> 00:02:52,000 |
| I will share with you source code of the solution for these tasks, but try to solve them by yourself |
|
|
| 43 |
| 00:02:52,000 --> 00:02:53,000 |
| first. |
|
|
| 44 |
| 00:02:53,000 --> 00:02:56,000 |
| Thanks a lot for your attention and see you in the next lesson. |
|
|
|
|