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

preview_player
Показать описание

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.

.
Please donate and support my work
(If you think my free tutorials are better than paid ones :)

Free Programming courses:

Free Flutter course:

Free Android courses:

More free programming courses:

Check out my website:

Let's get in touch! [Sriyank Siddhartha]

---- 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);
// 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:

Bunnys
Автор

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.

dinisalexandru-catalin
Автор

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.

raginigupta
Автор

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!

marvhartigan
Автор

Thank you for such nice and easy explanation finally i got the visual representation that i was looking for. Amazing.

arunbakshi
Автор

How come youtube teacher is much better than my actual teacher....

boeunchoi
Автор

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.

jimmyryan
Автор

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

ice_cube
Автор

This video is awesome. Keep posting such videos. Its really helpful

bogadipreethamreddy
Автор

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

nageensaira
Автор

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

senanup
Автор

Can you plz tell why you put the modify() in different block but not in the main method block.

salonivaish
Автор

Great video
Thanks for clearing by confusion!

Vimarsh
Автор

thank you! so that means we have to use cloning or make a new object inside the receiving method to change the object values

Adam-gpij
Автор

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

akashpol
Автор

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

prikshitverma
Автор

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

snehaparakkal
Автор

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

lazylizarts
Автор

Simply superb myan it's a beautiful explanation.

Srinivas_Konduri
Автор

Nice presentation I really understand thank you so much, ,

matthewjimoya