2 December 2020

Pass by value and pass-by-reference in Java. How do they work?

Welcome to another tutorial on the “How to code in Java” tutorial series. In the last tutorial, we’ve gone through on how static class members and constant variable works. In this tutorial, we’ll be going through on how passing-by-value and passing-by-references work in Java.

 

There are two types of data passing to a method in programming; the first one is passing-by value and the second one is passing-by-reference. Let’s look at their differences and when will they be used.

 

Index for the tutorial:


 

 

How passing-by-value work.

 

Passing-by-value means that the data will be copied when passed to a function or method. Any changes that are done to the copied value will not affect the original data.

 

Passing-by-value demonstration.

 

The code example for the passing-by-value.

The result for the code.


As you can see, when we print out the value in the method’s scope, the value can as expected the value 15 as the number 10 passed through as parameter is added with 5. When we print the value again after the method call ended and control returned to the main method (method execution finished), the value can be seen to be the original value 10 instead of the value 15 as of the change during the method execution.

 

At the end of the method call stack, all the value stored in the call stack will be destroyed as of Java call stack's natural. Thus any change of the value will not be retained after the call stack for that method finished. A more accurate word to be used would be that the value is popped out from the call stack, causing any change to the value to lose.

 

 

This is pretty much what does it means by copying a value and pass it into method/function. Any effect dealt with the copies will not affect the original data value.

 

Well, now I bet you’ll be wondering which data type will use this passing-by-value method when their value is passed as an argument for a method. For now, just think that every value that’s passed into a method used the passing-by-value method. Now, don’t panic for the moment and continue to read on, and you’ll found out why it operates that way.

 

How passing-by-reference work.

 

Unlike passing-by-value, when passing-by-reference is used to pass the value to the method, any changes made to the value will affect the original data value directly.

 

Just before this section where I mentioned that all argument passing in Java used passing-by-value to transfer the data. This is because in Java the reference to the memory location of the data is copied and passed to the method. Thus is also count as passing-by-value, the reference value is passed to the method as the argument.

 

Passing-by-reference demonstration

 

Now, let’s demonstrate the passing of the object reference to the method.

 

The example for passing-by-reference (part 1)


The example for passing-by-reference (part 2)

Result for the example code.

As you can see, by changing the value of the object’s “value” field value, the change affects the original object directly. Even though all the variable in the method will be destroyed after the method finished execution and removed from the memory, since the change is directly changing the value from its memory, the change will not be lost.


Now, what do you think will happen if we assign a new object into the object and then change its value? Using back the code just now and with some changes to the changeObjectValue() method.

 

Created a new object inside the method. 

The object in the main method didn't get affected.


As you can see, the value of the first object does not change after we performed a change to the parameter object value. This is because a new object reference is assigned to the obj variable, which causes any change affecting the variable to be directed to the newly created object instead of the object declared in the main method.

 

That’s about all the things that I could tell you regarding passing-by-value and passing-by-references. I hope you now know how does the data is passed when passing arguments into methods. If you have any questions, suggestions, or critics, feel free to leave a comment in the comment section below. In the next tutorial, we’ll be looking into variable scope before going into our two OOP concepts, Abstraction and Encapsulation. So stay tuned.

No comments:

Post a Comment