File size: 7,868 Bytes
33852e5 | 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | 1
00:00:00,000 --> 00:00:01,000
Hello team!
2
00:00:01,000 --> 00:00:05,000
In this lesson we will discuss with you a very important topic which even some more experienced software
3
00:00:05,000 --> 00:00:08,000
engineers can't understand.
4
00:00:08,000 --> 00:00:12,000
But I will explain you this topic with simple examples and you will remember it easily.
5
00:00:13,000 --> 00:00:18,000
Today we will talk with you how data is passed to the method in Java by value or by reference.
6
00:00:18,000 --> 00:00:18,000
Why?
7
00:00:18,000 --> 00:00:23,000
This question is important because in different programming languages, you can decide whether you want
8
00:00:23,000 --> 00:00:26,000
to pass value to the method by value or by reference.
9
00:00:27,000 --> 00:00:33,000
This might affect the state of the data which was supposed to be modified after specific method call.
10
00:00:33,000 --> 00:00:38,000
So you already understand that we can pass arguments by value and by reference.
11
00:00:38,000 --> 00:00:40,000
What does pass by value mean?
12
00:00:40,000 --> 00:00:46,000
Passing by value means that the value of the method parameter is copied into another location of your
13
00:00:46,000 --> 00:00:52,000
memory, and when accessing or modifying the variable within your function, only the copy is accessed
14
00:00:52,000 --> 00:00:55,000
or modified and the original value is left untouched.
15
00:00:56,000 --> 00:01:01,000
Imagine empty cup and specific function which you want to use to fill this cup with coffee.
16
00:01:01,000 --> 00:01:08,000
In case we pass by value, we copy this cup inside the function and all changes with this cup affects
17
00:01:08,000 --> 00:01:10,000
only cup which was copied.
18
00:01:11,000 --> 00:01:13,000
Then what does pass by reference mean?
19
00:01:14,000 --> 00:01:18,000
Passing by reference means that the memory address of the variable, or it is better to say a pointer
20
00:01:18,000 --> 00:01:21,000
to the memory location is passed to the method.
21
00:01:22,000 --> 00:01:26,000
In case we pass by reference, we pass the address where this cup is located.
22
00:01:26,000 --> 00:01:31,000
And when I fill the cup inside the function, I also fill the original cup.
23
00:01:31,000 --> 00:01:34,000
And now please remember the rule in Java.
24
00:01:34,000 --> 00:01:38,000
Primitive and reference types of data are passed by value.
25
00:01:38,000 --> 00:01:41,000
That is the only right answer on the interview.
26
00:01:41,000 --> 00:01:46,000
You can read in internet that Java objects are passed by reference, and primitive types are passed
27
00:01:46,000 --> 00:01:46,000
by values.
28
00:01:46,000 --> 00:01:49,000
That is not true on the simple examples.
29
00:01:49,000 --> 00:01:50,000
I will prove you this.
30
00:01:51,000 --> 00:01:54,000
Here I have int value which is equal to ten.
31
00:01:54,000 --> 00:01:57,000
I have method which takes int as a parameter inside it.
32
00:01:57,000 --> 00:01:59,000
We want to increase I by 100.
33
00:02:00,000 --> 00:02:02,000
What value of I will be in console here?
34
00:02:02,000 --> 00:02:05,000
After the method call, the value will be ten.
35
00:02:05,000 --> 00:02:06,000
Why?
36
00:02:06,000 --> 00:02:09,000
Because in Java everything is passed by value.
37
00:02:09,000 --> 00:02:16,000
Here when I called method, the copy of I variable was created and when I increased I by 100 value,
38
00:02:16,000 --> 00:02:20,000
the result was assigned to the local I variable, but not to this one.
39
00:02:21,000 --> 00:02:22,000
These two are different.
40
00:02:23,000 --> 00:02:28,000
They remember in the lesson when we discussed Java memory model, I told you about stack memory.
41
00:02:28,000 --> 00:02:32,000
When this method is called copy of the I was created in stack.
42
00:02:32,000 --> 00:02:35,000
So in this case how I can change the value of I.
43
00:02:35,000 --> 00:02:39,000
Here the only way to do this in Java is to use return statement.
44
00:02:39,000 --> 00:02:43,000
Your message should return some value which will be assigned to the variable.
45
00:02:43,000 --> 00:02:52,000
Like in this example I call method change int, I add 100 and I return the value from this method which
46
00:02:52,000 --> 00:02:53,000
is assigned to I variable.
47
00:02:53,000 --> 00:02:54,000
Here.
48
00:02:56,000 --> 00:02:57,000
Let's move on.
49
00:02:58,000 --> 00:03:03,000
Now I have array of ints and you remember that arrays are reference type of data.
50
00:03:03,000 --> 00:03:07,000
I have method which has some logic inside to change state of the array.
51
00:03:08,000 --> 00:03:14,000
I want to make second element in this array equal to 200, but not two as in original array.
52
00:03:14,000 --> 00:03:17,000
Let me print this array to console after this method call.
53
00:03:17,000 --> 00:03:20,000
What do you think will be the second element?
54
00:03:20,000 --> 00:03:21,000
2 or 200.
55
00:03:22,000 --> 00:03:23,000
And it is 200.
56
00:03:24,000 --> 00:03:29,000
You might be wondering how this could happen if in Java we pass everything by value, then modification
57
00:03:29,000 --> 00:03:33,000
which we did in the method should not affect original array.
58
00:03:33,000 --> 00:03:35,000
Let's understand what has happened here.
59
00:03:35,000 --> 00:03:39,000
I call the method and pass reference to the array object.
60
00:03:39,000 --> 00:03:40,000
Attention.
61
00:03:40,000 --> 00:03:47,000
I pass the reference to the array, but not the array, and copy of the reference is created here.
62
00:03:47,000 --> 00:03:50,000
I can get access to the array through the reference.
63
00:03:50,000 --> 00:03:55,000
So here I use the reference to get access to the same array as here.
64
00:03:55,000 --> 00:04:00,000
These two variables reference to the same array object in the heap memory.
65
00:04:01,000 --> 00:04:06,000
Probably now you want to get proofs that this is a copy of reference, but not copy of the array.
66
00:04:06,000 --> 00:04:09,000
I have another method with name clear array.
67
00:04:09,000 --> 00:04:13,000
It takes an array as a parameter and makes this variable reference to null.
68
00:04:13,000 --> 00:04:16,000
Let me call this method with our array.
69
00:04:16,000 --> 00:04:18,000
Now I print it to console.
70
00:04:18,000 --> 00:04:22,000
What will be printed to console null or values from array?
71
00:04:22,000 --> 00:04:24,000
Values from array is on place.
72
00:04:25,000 --> 00:04:25,000
Why?
73
00:04:25,000 --> 00:04:32,000
Because this is copy of reference and now local method variable which exists only in scope of this method.
74
00:04:32,000 --> 00:04:33,000
References to null.
75
00:04:33,000 --> 00:04:35,000
No problems at all.
76
00:04:35,000 --> 00:04:39,000
This reference still refers to the object on heap memory.
77
00:04:39,000 --> 00:04:45,000
And now you can be sure that even when we pass objects in Java to the method, we still pass by value
78
00:04:45,000 --> 00:04:51,000
because we pass reference to the object and we pass value of the reference.
79
00:04:51,000 --> 00:04:55,000
Hope examples from this lesson help you understand the topic better.
80
00:04:56,000 --> 00:04:58,000
Let's recap what we have learned today.
81
00:04:58,000 --> 00:05:02,000
Today we learned what does pass by value and pass by reference mean.
82
00:05:02,000 --> 00:05:06,000
We also understood that in Java everything is passed by value.
83
00:05:06,000 --> 00:05:10,000
On the examples, we saw how primitive and reference types passed by value.
84
00:05:11,000 --> 00:05:12,000
That's all for today.
85
00:05:12,000 --> 00:05:14,000
Thanks a lot for your attention.
86
00:05:14,000 --> 00:05:16,000
See you in the next lesson.
87
00:05:16,000 --> 00:05:16,000
Your.
|