00:00Now let's talk about another important concept which is call by value.
00:05First, we will write two numbers in our main function.
00:10What does it mean to swap?
00:12It means to exchange values.
00:15You have two variables A and B.
00:18If you add A value to B and B value to A,
00:21then we will say swap or swap.
00:25I have integer A which is 5.
00:28The other integer B which is 10.
00:31Now I have to add A value to B and B value to A.
00:35For that, I will write swap code.
00:38First, I will create a temporary variable in which I will store A value.
00:42Then I will store B value in A.
00:46Then I will store B value in B.
00:49After that, if I will print values,
00:52what will I print?
00:54A is equal to, here is A value.
00:57And B is equal to, here is B value.
01:03Let's do this.
01:04Save.
01:05Run.
01:07If I should add A values,
01:09I will have a value.
01:10Then I will have a value with A value of 10 to 5.
01:12To inf leading a lack of time
01:13When I Vadhe ends, I have read a value with A in the end.
01:14A was equal to 5.
01:16After I had done a limiting five lines in B.
01:19How does the swap points feature in the end?
01:22First, we can intoด terms of a variable as a bucket.
01:24This was my temporary bucket, this was my bucket A and this was my bucket B. Now, in bucket A, I stored the value of 5 and in bucket B, I stored the value of 10 and the bucket is temporary.
01:40So, in the first statement, what did I do from int temp is equal to A? I added a value in the temporary.
02:00So, A will be empty and when the value of B will come into A, it will store the value of 10. Then, in the third line, I added a temporary value to B.
02:11So, what will happen? B will be empty and the temporary value will come into B. So, we will write 5.
02:19So, when I print A, I will print 10 and B will print 5. So, in this way, I swapped the value.
02:27How did I swap? With a third empty value of help.
02:30Now, for this swapping work, if I want to do a function, what will be the proper way of doing it?
02:37The proper way is that we write public static, then we write the return type of function which is void and then its name is swap.
02:45In the swap, I pass two input values or parameters A and B.
02:51Now, my job is swap both values.
02:54So, what can I simply do? I can remove this whole code and I can write it here.
02:59So, this is a swapping code, which I have swapped the values of A and B.
03:05I have swapped the values here.
03:06And with the main function, I have added a call.
03:09Swap function, which I have passed the first parameter A.
03:14As an argument, value 5, which I have passed here.
03:19And then I have passed B, as 10, which I have passed here.
03:25I will do this save and I will do this run.
03:27So, A's value 10, B's value 5.
03:32A's value 5.
03:33A's value 5.
03:34Now, swap 10, B's value 5.
03:36This code will work completely right.
03:39But, if I print the value of A and B's value, I want to print it in my main function.
03:46Then, what will my output come?
03:48After that, my output comes from original values of A and B.
03:52Why is that?
03:53That's why it happened.
03:55Because, in my function, I have written parameters int A and int B.
04:01These parameters, these two arguments make a copy.
04:06We call them call by value.
04:09Call by value means that if you have passed a value in a function,
04:14I have passed these variables in a function.
04:17So, whatever the change will be, it will only remain in a function.
04:21It will only remain in a swap function.
04:23The change will not remain in the main function.
04:27If A's value changed in this swap function,
04:31then the whole function will remain in the whole function.
04:33But, as the control will remain in the main function,
04:37A will get its original value.
04:40The way to visualize it is that if I have written a main function,
04:45let's say I have made a function change A.
04:50In the change A, I will take a variable int A,
04:53which I will change the value by 10.
04:56Then, I have a main function.
04:58Public static void main.
05:00What do I have done?
05:02In which I have defined an integer A.
05:05This variable, which I have initialized with 5.
05:08After that, I have called change A, which I have passed my A.
05:12And finally, what do I have done?
05:14Finally, I will try to print my variable A's value.
05:20So, in Java, how will they be?
05:22In Java, the main function is the main function.
05:24First of all, there will be a variable A.
05:26And when the change A will be called,
05:29then what will happen?
05:31In memory, the main function is the main function.
05:36There is a variable in which the value is stored.
05:41As the main function will call change A,
05:44then what will happen in the call stack?
05:46In the call stack, the change A will come.
05:49And in change A, the variable is a new variable.
05:54Meaning, this parameter will become a copy here.
05:59So, A will come.
06:01The value will store 5.
06:03So, the formal parameter is a copy here.
06:09So, when I pass the function to a value,
06:12then the value is a copy of my original value.
06:15I never pass my original A.
06:17I always pass my original A copy.
06:20So, as I have passed that copy,
06:22then another variable A has become a name.
06:25The value will store 5.
06:28And then this function has a is equal to 10.
06:31So, the change in this A is equal to 10.
06:35So, until I am working in this function,
06:38then I will use this value.
06:40But, as I have returned here,
06:42what happened?
06:44This call stack has come up.
06:46And my changed value was 10.
06:48It also has come up with the call stack.
06:49So, if I will print A,
06:51then I will print this A is equal to 5.
06:54So, this is the original value.
06:56So, this is the concept of call by value.
06:58The concept of call by value is that
07:00in Java,
07:01if you pass some value in Java,
07:04then it will not pass the original variable.
07:06There is a copy of the original variable.
07:09And it will copy to your other functions.
07:12Like I had this function A,
07:14so, what has passed here,
07:16this A is the copy of this A.
07:20It is not original A.
07:21In Java, all the functions
07:24always work on call by value.
07:27Those functions,
07:28which you pass the original value,
07:30we will call by reference.
07:32We will see the call by reference functions.
07:34We will see C++ in C++.
07:36In Java,
07:37all the functions
07:39we are executing with call by value.
07:41That means,
07:42every time,
07:43the variable is copied
07:44into the function call.
07:45So, this concept
07:46is very important.
07:47Whenever you have a question,
07:49in an interview,
07:51if you are sitting in theoretical questions
07:53in interviews
07:54or your interviewer
07:56then you can ask
07:58that in Java,
07:59if there is a value
08:00in another function
08:01that is called by value
08:03that is called by reference.
08:04References,
08:05that is the original thing
08:06that is called by value,
08:08that is only value
08:09given.
08:10That means,
08:11that is not given the original thing,
08:12that is the value of it.
08:13So, in a way,
08:14it is copied.
08:15It is in a way that is able
08:16as possible,
08:17but the value of it will
08:18have the value equal to value.
08:20Even at that time,
08:21at that time,
08:22if you have it in a way that this
08:23person takes a project
08:25that will correct this.
08:26That means the value for
08:27the positive thing of the
08:27ANSYCE
08:30last thing so that
08:31that is allowed to market
Comments