File size: 4,531 Bytes
24fed34 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | 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.
|