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
@technolozieskart6 жыл бұрын
kzbin.info/www/bejne/mKm7iItjZ9B_m5o
@preparingfortheendofthewor95135 жыл бұрын
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: kzbin.info/www/bejne/hGmsloCPqMmad7s kzbin.info/www/bejne/nmrYdWqfpdaoadk kzbin.info/www/bejne/eHnXl5VpocuWY8k kzbin.info/www/bejne/o3LSe6eigculp8k
@golubhai19102 жыл бұрын
yeah i was also confused when he said its call by reference, when it is clearly call by value
@paulhetherington3854 Жыл бұрын
You didn't - research itt'? Industry&System - law. You didn't mean.
@dinisalexandru-catalin77026 жыл бұрын
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.
@yahyaalisheikh53793 жыл бұрын
Easiest xplanation ...... it's very helpful... lots of love
@boeunchoi98294 жыл бұрын
How come youtube teacher is much better than my actual teacher....
@marvhartigan36773 жыл бұрын
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!
@mistein8374 жыл бұрын
side note : Java is always call by value.
@nikhilm42903 жыл бұрын
call by value for primitives, call by reference for objects.
@johnjoseph44103 жыл бұрын
@@nikhilm4290 no it is always call by value
@physicswallahunofficial94023 жыл бұрын
Don't you have ever seen pasing refrence variable pointing to object.
@rosh93183 жыл бұрын
Sidenote.Value="Always call by reference" :)
@Its_lisha473 жыл бұрын
Side note : Java can't pass values
@fardilviews3 жыл бұрын
Thanks Love from Bangladesh.
@Srinivas_Konduri2 жыл бұрын
Simply superb myan it's a beautiful explanation.
@raginigupta98163 жыл бұрын
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.
@arunbakshi71386 жыл бұрын
Thank you for such nice and easy explanation finally i got the visual representation that i was looking for. Amazing.
@golubhai19102 жыл бұрын
java doesnt have call by reference and even then he explained it must be really smart guy
@jimmychoo90012 жыл бұрын
Thank you smart Indian man
@asmamunsor59623 жыл бұрын
thanks a lot I watched a lot of videos to understand but i didnt unless with u thank u so much
@akashpol024 жыл бұрын
Now cleared this concept...Thanks !!!!
@mrunalirajkule6442 жыл бұрын
Great explaination!!! Very helpful
@jimmyryan58806 жыл бұрын
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.
@ice_cube9183 жыл бұрын
Very helpful! Great and clear presentation. Finally understood this concept.
@anilmaurya553 жыл бұрын
Very clear explanation thanks
@nageensaira89214 жыл бұрын
i really impress the way u explain the things ,that's amazing.
@md.kh.s4 жыл бұрын
thanks from Bangladesh
@senanup5 жыл бұрын
Very good and simpler way of explanation. Tremendously liked it..
@akshaymainkar92453 жыл бұрын
Excellent Explaination!!
@positivepayal73 жыл бұрын
Nicely explained! 🙌👏
@lazylizarts2 жыл бұрын
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++
@bogadipreethamreddy51147 жыл бұрын
This video is awesome. Keep posting such videos. Its really helpful
@eknathbhoir61264 жыл бұрын
Very nice explanation
@matthewjimoya96214 жыл бұрын
Nice presentation I really understand thank you so much,,
@jagmohanlalsachan84644 жыл бұрын
Thanks you cleared my doubts
@ankit82694 жыл бұрын
Wooooow Thankss bhaiii
@Vimarsh245 жыл бұрын
Great video Thanks for clearing by confusion!
@ashishsahu13102 жыл бұрын
Fantastic!!
@NeverSayNever77 Жыл бұрын
Nice explained
@youtubeaccount0x0735 жыл бұрын
You get a like and subscribe! Best description
@manojreddy80505 жыл бұрын
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
@manojreddy80504 жыл бұрын
@@codersclub5016 i didn't cracked that interview 😣
@manojreddy80504 жыл бұрын
@@codersclub5016 i had selected in another one just 7 days after completion of that interview 👻👻👻
@sksubhani45324 жыл бұрын
great Explanation
@prikshitverma38854 жыл бұрын
Thanks buddy!!! Now days i am learning java concept .so that i can change my current job profile...any suggestions please help me...
@sahanharithapriya7533 жыл бұрын
Well Explained !
@guyswapnil3 жыл бұрын
Good Explanation
@Adam-gp3ij5 жыл бұрын
thank you! so that means we have to use cloning or make a new object inside the receiving method to change the object values
@marvhartigan36773 жыл бұрын
yeah
@sachinchinnannavar95145 жыл бұрын
very nice thankyou .i got it
@Tracks7777 жыл бұрын
Nice video!
@manasammisetty2784 Жыл бұрын
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.
@hrishikesh12757 жыл бұрын
good useful......better used.....best ever.......
@smartherd7 жыл бұрын
thank you very much 😉
@migene15436 жыл бұрын
Nice explanation! 👍
@sunilgupta91017 жыл бұрын
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.
@pranithagunukula49956 жыл бұрын
Java supports call by reference
@technolozieskart6 жыл бұрын
kzbin.info/www/bejne/mKm7iItjZ9B_m5o
@urvitandon38105 жыл бұрын
Both
@tejasbhatkal15973 жыл бұрын
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
@abduldaniq50043 жыл бұрын
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
@starktshering40435 жыл бұрын
nice explanation. ☺ thank you👑
@vardaan06124 жыл бұрын
Impressive 👏
@Whatt_Am_I_Doing6 жыл бұрын
well explained Sriyank
@vishalshinde52527 жыл бұрын
Well explained
@smartherd7 жыл бұрын
+Vishal Shinde thanks alot
@rabiteja52312 жыл бұрын
Thank you bro
@jayakumarnachimuthu62247 жыл бұрын
Nice Explanation ...
@snehaparakkal29013 жыл бұрын
Can we do arithmetic operations using this call by value and call by reference?
@sayannandy20306 жыл бұрын
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
@BlindCharades6 жыл бұрын
look at the line modify(r1)
@technolozieskart6 жыл бұрын
kzbin.info/www/bejne/mKm7iItjZ9B_m5o
@vinaysingh29716 жыл бұрын
bahut badhiya
@salonivaish57405 жыл бұрын
Can you plz tell why you put the modify() in different block but not in the main method block.
@priyasuryawanshi11436 жыл бұрын
Excellent
@prikshitverma38854 жыл бұрын
Why we do not use call by reference in java? I just read somewhere....can u explain plz
@vandananayaknayak55115 жыл бұрын
thanks! this is helpful
@arpitpatel48256 ай бұрын
Thanks
@nageensaira89214 жыл бұрын
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.
@tahahussain52814 жыл бұрын
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
@Murtazakhan--3 жыл бұрын
thanks mann
@thoughtsofthetravel4 жыл бұрын
why the voice is so familiar, i heard the same voice in internshalla c++ course
@smartherd4 жыл бұрын
That was amazing catch. That course was created by me 😃
@thoughtsofthetravel4 жыл бұрын
@@smartherd oh really ,your voice is amazing ,and you explained c++ very smoothly, but i still didnt got the marks for my last assignment 😂
@sukhmanpreetsingh5956 жыл бұрын
Veryyy Helpful
@pranavwagh93503 жыл бұрын
presentation is good...bt java is ALWAYS call by value
@AyushGupta-nr2un7 жыл бұрын
is it for class 12 ip ?
@vamshimedishetty56246 жыл бұрын
What trigger means?
@smartherd6 жыл бұрын
Execution
@vamshimedishetty56246 жыл бұрын
Tnks for replying immediately
@ujjwalsingh26 жыл бұрын
Right?
@shashankvishwakarma9593 жыл бұрын
you didn't explain why java is call by value...
@someoneintheshadow457 жыл бұрын
Thank you!!!
@codingschool85945 жыл бұрын
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.
@JeonJungkook-uw1dg5 жыл бұрын
Java supports call by reference...
@Darshil17715 жыл бұрын
@@JeonJungkook-uw1dg No it doesnt
@JeonJungkook-uw1dg5 жыл бұрын
@@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..😊😊
@kishorekumar27694 жыл бұрын
Bro then how the programme is executing without any issue.Please explain i am new to java.
@tagvalg4 жыл бұрын
great
@srinathnirmal8676 жыл бұрын
good
@MukeshYadav-pt2uc6 жыл бұрын
Java doesn't support call by reference
@hicham33417 жыл бұрын
goooooooooooood ++++++++++
@anonymousji77517 жыл бұрын
Confused 😓
@smartherd7 жыл бұрын
+Raghav Narang at what point you felt confused. Let me know
@nikeelafarhana8964 жыл бұрын
it would be nice if u explain somewhat slow
@varun4645 жыл бұрын
Dude... Stop teaching wrong concepts. Java supports only call by value and not call by reference.
@kishorekumar27694 жыл бұрын
Bro then how the programme is executing without any issue.Please explain i am new to java.