Java Call by Value vs Call by Reference. Object Oriented Java Tutorial: #12
ฝัง
- เผยแพร่เมื่อ 7 ก.ย. 2024
- Access 7000+ courses for 15 days FREE: pluralsight.px...
Java tutorial for beginners where I will talk about What is CALL BY VALUE and CALL BY REFERENCE, importance, significance and how does it takes place. Detail difference has been covered along with demo code.
Source code files: bit.ly/2MC04SR
Next Video : • Java STATIC keyword: S...
Previous Video : • Java CONSTRUCTORS, its... .
.
Please donate and support my work
(If you think my free tutorials are better than paid ones :)
- Patreon: bit.ly/patreon...
- Paypal/Payoneer: sriyank123@gmail.com
- UPI (only for India): smartherd@okaxis
:: If you want to develop a website or a mobile app, email me your requirement at sriyank.siddhartha@gmail.com :: Free demos provided beforehand ::
- Access my premium courses: bit.ly/sriyank...
Free Programming courses:
- Ruby Programming: bit.ly/smyt-r
- Dart Programming: bit.ly/smyt-d
- Kotlin Programming: bit.ly/smyt-k
- Java Programming: bit.ly/smyt-j
- Kotlin Coroutines: bit.ly/smyt-coru
Free Flutter course:
- Flutter App Development: bit.ly/2Rg7EFR
Free Android courses:
- Android using Kotlin: bit.ly/smyt-ka
- Android using Java: bit.ly/smyt-ja
- Android Material Design: bit.ly/2SMJqU6
- Android Jetpack Architecture: bit.ly/yt-j
- Android Multiple Screen Support: bit.ly/smyt-mss
- Android Retrofit: bit.ly/2Ee6GHn
More free programming courses:
- bit.ly/smy-list
Check out my website:
- bit.ly/smartherd
Let's get in touch! [Sriyank Siddhartha]
LinkedIn: bit.ly/sriyank...
Facebook: bit.ly/smarthe...
Instagram: bit.ly/sriyank...
Twitter: bit.ly/sriyank...
Github: bit.ly/smarthe...
--- Thank you for your love and support ---
While the examples are correct and you correctly stated that "r1" and "r2" are also located on the stack, both of your examples are *call by value* . The Java language doesn't have the concept of passing a *variable* by reference. People always confuse "reference types" or "references" with the concept of passing a variable by reference.
Here's an actual example that shows that the *variable* is actually passed by value. So inside your method you get a *copy* of the variable and not a reference:
Rectangle r1 = new Rectangle();
r1.length = 10;
modify(r1);
System.out.println(r1.length); // prints "10"
void modify(Rectangle r2)
{
r2 = new Rectangle();
r2.length = 20;
}
As you can see the *variable* is actually *passed by value*. Because *changing the variable* inside the method has no effect on the *variable* that was passed in. The "value" of r1 is copied into r2. However since Rectangle is a reference type, the value of the variable is just an address, the reference to the actual object on the Heap.
Again, this is *not* called pass by reference.
C# for example does have true reference parameters. So when calling the method you don't copy the value of the variable but instead you pass a reference / pointer to the variable location on the stack.
//C#
Rectangle r1 = new Rectangle();
r1.length = 10;
modify(ref r1);
print(r1.length); // prints "20"
void modify(ref Rectangle r2)
{
r2 = new Rectangle(); // we change the variable r2
r2.length = 20;
}
Of course if you're a pure Java programmer you don't really come in contact with the concept of passing by reference (as it's not supported by Java). However calling "using a parameter to access the referenced object" as "call by reference" is just wrong. See wikipedia:
en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference
th-cam.com/video/bxXWX-2kLe8/w-d-xo.html
Bunny83 You are absolutely right. I finally understand better the concept of why and how Java always passes by value after watching these four videos:
th-cam.com/video/N8IeMYsdgAY/w-d-xo.html
th-cam.com/video/h9uD7ipqu3w/w-d-xo.html
th-cam.com/video/BHtfb3lfc-g/w-d-xo.html
th-cam.com/video/mAoJtlLfrqg/w-d-xo.html
yeah i was also confused when he said its call by reference, when it is clearly call by value
You didn't - research itt'? Industry&System - law. You didn't mean.
The explain is partially correct, and actually it shows that Java only supports pass by value. When sending the parameter to the function, both of them are pointing to the actual object. So what' s changed it's actually the object, not the reference. Try to set the reference to null inside the modify method. If pass by reference, the value of r2 should be null, so you should get a NullPointerException.
Thank you for such nice and easy explanation finally i got the visual representation that i was looking for. Amazing.
java doesnt have call by reference and even then he explained it must be really smart guy
How come youtube teacher is much better than my actual teacher....
Static variable, method and block store in stack memory automatically.
But Instance variable, method and constructor store in heap memory when we create an object.
very good, one of the few explanations which easily make sense even for me.Thanks a lot, this channel is gonna blow up soon too im sure!
side note : Java is always call by value.
call by value for primitives, call by reference for objects.
@@nikhilm4290 no it is always call by value
Don't you have ever seen pasing refrence variable pointing to object.
Sidenote.Value="Always call by reference" :)
Side note : Java can't pass values
Easiest xplanation ...... it's very helpful... lots of love
Great video. Very clear. What I don't understand is any time I look this answer up all the answers I find contradict themselves. They say java ALWAYS passes by value (they emphasise this) and then go onto explain how it passes objects by reference.
Thank you smart Indian man
Simply superb myan it's a beautiful explanation.
thanks a lot I watched a lot of videos to understand but i didnt unless with u thank u so much
i really impress the way u explain the things ,that's amazing.
here the moment u call r2 in stack its point to the same object in the heap, hence when u print the r1.lenght it point to the object D1 with a changed length, so the value is not passed by reference like c++
Very helpful! Great and clear presentation. Finally understood this concept.
Thanks Love from Bangladesh.
Now cleared this concept...Thanks !!!!
thanks from Bangladesh
Very good and simpler way of explanation. Tremendously liked it..
Nice explained
Very clear explanation thanks
thank you! so that means we have to use cloning or make a new object inside the receiving method to change the object values
yeah
Nice presentation I really understand thank you so much,,
Great explaination!!! Very helpful
Thank you bro
Fantastic!!
This video is awesome. Keep posting such videos. Its really helpful
Great video
Thanks for clearing by confusion!
Thanks buddy!!! Now days i am learning java concept .so that i can change my current job profile...any suggestions please help me...
Excellent Explaination!!
Thanks
Thanks you cleared my doubts
Nice video!
Nicely explained! 🙌👏
Very nice explanation
Well explained
+Vishal Shinde thanks alot
great Explanation
Well Explained !
Excellent
Good Explanation
Hi, this is Strings and dharth and welcome to the next video of this series of Java fundamentals and basics. In this video, I will talk about what is the meaning of call by value and what is the meaning of call by reference. Now as a beginner in programming, you should be aware of these two concepts quite well. Because these are the concepts that are universal to the whole programming world. So if you are clear with this concept of value and reference, then you won't face any difficulty in the world of coding, especially in terms of object oriented programming. So let us now proceed and check out some example. So first of all, what is call by value? Now suppose here I have the method of modify. So I will pass some data and I will simply modify this integer variable inside this method. So inside the main method, suppose I have integer x equal to 10. Now remember, this integer is actually primitive data type. So whenever we create a primitive data type, the Java virtual machine, what it do, it simply creates the integer x inside the stack memory. Now you already know what is a heap memory from the previous videos. Now here whenever we create the primitive data type, it is actually stored inside the stack memory. So here inside the stack, we have integer x equal to 10. Now next, suppose I call modify and simply pass x as a parameter. So when this statement is executed, this method is triggered. Now when this method is triggered, here we have integer data. So this statement, when it is executed in data, it will simply get the value of x, right? No doubt here as well, but here is the twist. When this integer data will get the value of x, the Java virtual machine will simply create one more variable of in data inside the stack. So inside the stack, we have two variables in x and in data. Now both have the value of 10 and 10 respectively. Now inside the modify method, suppose if I change data equal to 20. So what will happen inside the memory? Will it impact the x variable as well? The answer is no. Because inside the stack memory, we have integer data and integer x defined separately. So inside the stack, we will have data equal to 20 because this statement has been executed. So now this statement will have no impact on the value of x variable. So now proceeding forward, suppose if I print out the value of x, then in the output console, we will get 10. Just because we are having the call by value, that is, the value is being transferred to some other primitive data type, right? But now what is the difference between the call by value and call by reference? So let us first understand what is the call by reference. Now whenever we create a object or we deal with some reference type variables, the concept of heap comes into picture. Now this is the heap memory where all the objects are actually stored. Now again here we have this method, public void modify. Now here I have rectangle R2. Now if you compare the right hand side with the left hand side, here we had the primitive type of data. But here on the right hand side, we have the reference type of variable or you can say this variable is of reference type, which is going to point to some object, right? So here inside the main method, suppose if I define rectangle R1 equal to new rectangle, that is, we simply create a reference variable that is pointing to some object, that is, new rectangle is the object. Now this R has to be capital, right? It doesn't matter as of now. Now in the very next step, suppose if I call R1 dot length equal to 10, that is, I set the length of this R1 object to 10. So when this statement is executed, then it will simply create a new object inside the heap memory. Let us call it D1. And now for this object D1, we have the R1 as the reference variable whose length is 10. So this R1 will now again be present as a reference variable and it will simply point towards D1. Now this R1, similar to the primitive data type, is also stored inside the stack memory. Fine. So there is a difference between the stack memory and heap memory. In the heap memory, we have all the objects and in the stack memory, we have the reference variable or the primitive data types, right? So inside the stack memory, currently we have R1 available. Now in the next step, suppose if I call modify method and simply pass R1 as a parameter. Now this time, the scenario will be a little different. So here R1 will be passed, but here we are creating we have object type reference variable rectangle space R2. So what the Java virtual machine will do, it will simply create one more reference variable R2 and store it inside the stack memory. Right. Similar to what we saw in the left hand side, but the difference arise now. This R2 will simply point to the same object of D1. Because we are not creating any new object here, we are simply making R2 to point towards the same object to which R1 is pointing, that is D1. So both R1 and R2 are pointing towards the same object D1. And now inside the modify method, if I change R2 dot length equal to 20. So since R2 is pointing towards D1, so whatever changes we are making in R2 will be reflected inside the object. And now the same object is being pointed out by R1. So whatever changes we will make inside the R2 will be reflected inside the R1. So now let us print the value of R1 dot length. Now this time we get the output as 20 instead of 10. So compared to the left hand side, you can see we got the output of 10. But now here we got the output of 20. So this is the difference between the call by value and call by reference. So in call by value, we actually transfer the value, but in call by reference, we actually make another reference variable pointing to the same object. So here R1 and R2, the names are different, but whatever changes we will make in either of these two will be reflected inside the D1. And henceforth we will get the value of R1 and R2 as same, right? So let us check out this concept inside the intelligent IDE. So here I have this method public static void modify, fine. Now this rectangle X, let's change it to let's say R2. And here let's change it to R2 as well. And down the side I have the class of rectangle that has a field variable of length and breadth. And down the side we have some constructors, let's remove it, which we used actually in the previous videos, right? So let's remove it and down the side we have some getters and setters. So let it be like this only. And now at the top here we are creating R1 as a reference variable and here this is the new object being created. And now what I will do I will simply call R1 dot length, you can also use the setter equal to let's say 10.0 if. And now what I will do I will simply call this method modify and simply pass R1 as a parameter. Now here inside this what do we have is we have rectangle R2. So this is again the reference variable. Now if I pass a reference here and here again we have a reference. So we have the example of call by reference. So these two R1 and R2 will simply point to the same object. So if we modify R2 dot length equal to 40, then it will simply modify the value inside the R1 as well because both are pointing towards the same object. And now suppose if I print s out R1 dot length and also s out R2 dot length. And let's cut it from here and paste it inside this method, right? And now let us run our code and see what happens. And now here we get 40.0 and 40.0 as the output. So in both the case of R2 and R1 the value of length is same. So this simply shows that we are using call by reference here. Now let us try something different with the help of primitive data type. So let us use call by value example. So here what I will do I will simply use int R1 equal to 10. And then I will simply call modify R1 and here I will change it to int, right? And modify the code here as well. So here this is our final example of call by value. Here we have integer R1 equal to 10. And then we call this method modify R1 where we have int R2. Now these two R1 and R2 will be stored inside the stack memory separately. So both these R1 and R2 are independent of each other. So if we modify R2 equal to 40, then and print the value of R2, then we will get 40 as the output. And here if you print R1, then we will get 10 as the output, right? So in both the case the value printed will be different. So let us now run our code. So here we get the value of R2 as 40 and R1 as 10, R2 as 40 and R1 as 10, fine. So this was all about the call by value and call by reference. Now always remember this. So this is all for this video, this is again straight from smart herd signing off. And please subscribe, like and comment below this video. Thanks for watching. See you guys in the next video. Thank you.
thanks mann
very nice thankyou .i got it
Very helpful ! Thanks.
Wooooow Thankss bhaiii
good useful......better used.....best ever.......
thank you very much 😉
You get a like and subscribe! Best description
nice explanation. ☺ thank you👑
ohh man..explanation is super....I have an Interview tomorrow, What if You comes as a Interviewer and asks this question. I will explain perfectly and I will get a job.....hahaa
@@codersclub5016 i didn't cracked that interview 😣
@@codersclub5016 i had selected in another one just 7 days after completion of that interview 👻👻👻
What happens in case of Sting yt is not a primitive data type?
String x = "X";
System.out.println(x +" x");
modify(x);
System.out.println(x +" x");
public static void modify(String y){
y = "Y";
System.out.println(y);
}
output:---
X x
Y
X x
String x ="X" will not create object on heap.. it will create the object only on SCP...
Try with String x=new String("X"). In this case , object will be created on both heap and scp
Nice explanation! 👍
Impressive 👏
Why we do not use call by reference in java? I just read somewhere....can u explain plz
Can you plz tell why you put the modify() in different block but not in the main method block.
Nice Explanation ...
I understood call by value and call by reference concept but please make me clear that what java support call by value or call by reference.
Java supports call by reference
th-cam.com/video/bxXWX-2kLe8/w-d-xo.html
Both
Can we do arithmetic operations using this call by value and call by reference?
Hi brother can you make a video on constructor in selenium ide like our class is A and i will make a constructor of class A(){} here i want to take a webdriver in parameter of constructor of class A(WebDriver){}then can you explain me what is the purpose of taking the webdriver in parameter of constructor.what will do this.if you see my comment plz reply me i will be very thankful to u.
Java does not supports call by reference because Java doesnt have pointer because of security breaks. It is also know as call by value instead of primitive value you pass obj value
thanks! this is helpful
well explained Sriyank
bahut badhiya
Veryyy Helpful
In the last program, why 40 i.e value of r2 came first while S.O.Pln of r1 was first on the program?
Plz explain me sir
look at the line modify(r1)
th-cam.com/video/bxXWX-2kLe8/w-d-xo.html
presentation is good...bt java is ALWAYS call by value
Thank you!!!
great
goooooooooooood ++++++++++
good
Wrong explanation. Java doesn't support call by reference. Since r1 and r2 are pointing to the same location. That's way the value of r1 also gets 20 on execution.
Java supports call by reference...
@@JeonJungkook-uw1dg No it doesnt
@@Darshil1771 it is given in my book, I m an icse class 10 student and it is in my book, that means what I have commented is true..😊😊
Bro then how the programme is executing without any issue.Please explain i am new to java.
you didn't explain why java is call by value...
is it for class 12 ip ?
Right?
why the voice is so familiar, i heard the same voice in internshalla c++ course
That was amazing catch. That course was created by me 😃
@@smartherd oh really ,your voice is amazing ,and you explained c++ very smoothly, but i still didnt got the marks for my last assignment 😂
What trigger means?
Execution
Tnks for replying immediately
Dude... Stop teaching wrong concepts. Java supports only call by value and not call by reference.
Bro then how the programme is executing without any issue.Please explain i am new to java.
Java doesn't support call by reference
it would be nice if u explain somewhat slow
Confused 😓
+Raghav Narang at what point you felt confused. Let me know
Confusing
Whr?
th-cam.com/video/bxXWX-2kLe8/w-d-xo.html
Nahi samja Kuch