Java Call by Value vs Call by Reference. Object Oriented Java Tutorial: #12

Java Call by Value vs Call by Reference. Object Oriented Java Tutorial: #12

Smartherd

7 лет назад

149,452 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Never Say Never
Never Say Never - 19.03.2023 18:00

Nice explained

Ответить
P'KTHERX'TON'
P'KTHERX'TON' - 19.02.2023 19:45

/3'Gtmv'''X''-callsz: 3''signae Vdx stckrs 3-D thrmsz({vktx'''6x slmrO})cg=Abysz''' HETH CAMz/
/<({3''3'3 2''2 2' 1'''1''1' r'''wt'''ma slmr signae cg Lx''' z''' aerospaax spidy weebelz diamsz foils FLIIRON/
/x''({3t'''3''vk GrrPsz 2'''Gtmv'''Vma slmr cg''' ++- y''})//Flaaksz foil arrays fly'tt' legsz 2'''e n lambda signae < G'=signae e n(2'')/

Ответить
Manas Ammisetty
Manas Ammisetty - 27.01.2023 15:31

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.

Ответить
Rabiteja
Rabiteja - 17.01.2023 18:05

Thank you bro

Ответить
Ashish Sahu
Ashish Sahu - 01.09.2022 09:32

Fantastic!!

Ответить
jimmychoo9001
jimmychoo9001 - 05.07.2022 00:49

Thank you smart Indian man

Ответить
Mrunali Rajkule
Mrunali Rajkule - 07.06.2022 16:23

Great explaination!!! Very helpful

Ответить
Srinivas
Srinivas - 24.05.2022 11:44

Simply superb myan it's a beautiful explanation.

Ответить
lazy lizard
lazy lizard - 15.05.2022 18:20

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++

Ответить
Pranav Wagh
Pranav Wagh - 28.01.2022 15:08

presentation is good...bt java is ALWAYS call by value

Ответить
asma munsor
asma munsor - 04.12.2021 00:36

thanks a lot I watched a lot of videos to understand but i didnt unless with u thank u so much

Ответить
Murtaza khan 02-134172-039
Murtaza khan 02-134172-039 - 06.10.2021 11:56

thanks mann

Ответить
Anil Kumar
Anil Kumar - 01.10.2021 23:52

Very clear explanation thanks

Ответить
Yahya Ali Sheikh
Yahya Ali Sheikh - 16.09.2021 19:07

Easiest xplanation ...... it's very helpful... lots of love

Ответить
Code Kiddo
Code Kiddo - 06.08.2021 10:19

Nicely explained! 🙌👏

Ответить
Shashank Vishwakarma
Shashank Vishwakarma - 17.07.2021 12:07

you didn't explain why java is call by value...

Ответить
Fardil_Views
Fardil_Views - 12.07.2021 05:48

Thanks Love from Bangladesh.

Ответить
Akshay Mainkar
Akshay Mainkar - 03.06.2021 06:22

Excellent Explaination!!

Ответить
ice_cube
ice_cube - 17.05.2021 23:29

Very helpful! Great and clear presentation. Finally understood this concept.

Ответить
tejas bhatkal
tejas bhatkal - 10.05.2021 12:00

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

Ответить
Sneha Parakkal
Sneha Parakkal - 07.04.2021 18:00

Can we do arithmetic operations using this call by value and call by reference?

Ответить
Swapnil Ghosh
Swapnil Ghosh - 09.03.2021 21:06

Good Explanation

Ответить
marv hartigan
marv hartigan - 01.03.2021 08:17

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!

Ответить
Ragini Gupta
Ragini Gupta - 09.02.2021 11:33

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.

Ответить
Sahan Harithapriya
Sahan Harithapriya - 06.02.2021 15:01

Well Explained !

Ответить
akash pol
akash pol - 27.01.2021 21:03

Now cleared this concept...Thanks !!!!

Ответить
Taha Hussain
Taha Hussain - 23.01.2021 15:43

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

Ответить
ankit
ankit - 19.01.2021 17:17

Wooooow Thankss bhaiii

Ответить
sk subhani
sk subhani - 15.12.2020 12:53

great Explanation

Ответить
Eknath Bhoir
Eknath Bhoir - 27.11.2020 07:33

Very nice explanation

Ответить
Nageen Saira
Nageen Saira - 13.11.2020 04:23

i really impress the way u explain the things ,that's amazing.

Ответить
Nageen Saira
Nageen Saira - 13.11.2020 04:19

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.

Ответить
Matthew Jimoya
Matthew Jimoya - 05.11.2020 11:20

Nice presentation I really understand thank you so much,,

Ответить
AI Dogesh
AI Dogesh - 13.10.2020 11:54

why the voice is so familiar, i heard the same voice in internshalla c++ course

Ответить
Vardaan
Vardaan - 18.08.2020 15:21

Impressive 👏

Ответить
Nikeela Farhana
Nikeela Farhana - 30.06.2020 17:22

it would be nice if u explain somewhat slow

Ответить
Jagmohan Lal Sachan
Jagmohan Lal Sachan - 22.06.2020 12:23

Thanks you cleared my doubts

Ответить
Mi Stein
Mi Stein - 20.06.2020 15:35

side note : Java is always call by value.

Ответить
Prikshit Verma
Prikshit Verma - 28.05.2020 20:20

Why we do not use call by reference in java? I just read somewhere....can u explain plz

Ответить
Prikshit Verma
Prikshit Verma - 28.05.2020 20:12

Thanks buddy!!! Now days i am learning java concept .so that i can change my current job profile...any suggestions please help me...

Ответить
Atharva. Chaphalkar
Atharva. Chaphalkar - 14.04.2020 11:19

Nahi samja Kuch

Ответить
Md. Khaled Hasan
Md. Khaled Hasan - 08.04.2020 17:53

thanks from Bangladesh

Ответить
TG
TG - 26.02.2020 08:02

great

Ответить
manoj reddy
manoj reddy - 26.01.2020 19:16

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

Ответить
Vandana Nayak Nayak
Vandana Nayak Nayak - 01.01.2020 21:53

thanks! this is helpful

Ответить
Anupam Sen
Anupam Sen - 11.12.2019 15:53

Very good and simpler way of explanation. Tremendously liked it..

Ответить
Vimarsh
Vimarsh - 29.11.2019 19:07

Great video
Thanks for clearing by confusion!

Ответить
Sachin Chinnannavar
Sachin Chinnannavar - 24.11.2019 14:40

very nice thankyou .i got it

Ответить
Varun Deoghare
Varun Deoghare - 16.09.2019 22:31

Dude... Stop teaching wrong concepts. Java supports only call by value and not call by reference.

Ответить