java-development-for-beginners-learnit / 10 - Methods in Java /002 Parameter Passing Mechanism in Java_en.srt
Tan115's picture
Add files using upload-large-folder tool
33852e5 verified
Raw
History Blame Contribute Delete
7.87 kB
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.