This post is about one of the basic question whether Java is pass by value or pass by reference. Though basic, this question causes some confusion mainly for objects passed as parameter. In this post let’s go through some examples to clarify the doubts and have a clear understanding of the topic.
To start with first have an understanding of the two terms used here pass by value and pass by reference.
- Pass by value- In pass by value the value assigned to the variable is copied to another variable. That another variable uses the copied value with in its scope without modifying the original value.
- Pass by reference- In pass by reference the alias to an original variable is passed to the method parameter.
Java is pass by value
In Java whatever type of data you pass whether primitive or an object, it is always pass by value. It is easy to understand for primitive values but a little confusing when it comes to objects. The confusion originates because of the terminology used, when object is passed as a parameters what we say is object reference is passed.
It’s high time to see some examples to get a clear picture.
Passing primitive as method parameter
public class PassValue { public static void main(String[] args) { PassValue pv = new PassValue(); int num = 7; System.out.println("Value before method call " + num); pv.displayValue(num); System.out.println("Value after method call " + num); } private void displayValue(int num){ // modifying the value num++; System.out.println("Changed value in method " + num); } }Output
Value before method call 7 Changed value in method 8 Value after method call 7
Here you can see that an integer variable is passed as a parameter to the method, in the method value of the passed parameter is changed but that doesn’t affect the original variable. That shows the actual variable’s value is copied to the parameter so it is pass by value.
Passing object as method parameter
Now comes the object part which needs some explanation. When you pass an object it is true that the reference to the memory allocated for that object is passed but there is a subtle difference when that reference is passed as value as compared to when it is passed as reference.
When object reference is passed as value the memory location stored in the variable is copied to the method parameter. Which means changing the object fields in the method will be reflected everywhere but changing the reference itself won’t change the reference for the original object.
But in case of pass by reference an alias is passed which means even the reference can be changed.
Example code showing object fields can be changed
public class PassValue { private int num; PassValue(int num){ this.num = num; } public static void main(String[] args) { PassValue pv = new PassValue(7); System.out.println("Value of num before method call " + pv.getNum()); pv.displayValue(pv); System.out.println("Value of num after method call " + pv.getNum()); } private void displayValue(PassValue obj){ // Changing field in the passed object obj.setNum(++num); System.out.println("Changed value in method " + obj.getNum()); } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }Output
Value of num before method call 7 Changed value in method 8 Value of num after method call 8
In the above code you can see that the object is passed to the displayValue() method and there the fields num is modified. The output shows that the field is changed in the calling method too.
This statement PassValue pv = new PassValue(7);
creates a new object of class PassValue and
initializes it to have value 7 for variable num. Here pv is the variable that stores
the reference to that created object.
When you call method in this statement – pv.displayValue(pv);
the object reference stored in
pv is copied to the obj parameter. Both pv and obj
share the same reference.
As shown in the image both variables pv and obj share the same memory reference so the change made to the field num using object obj is visible when num field is retrieved using pv object.
Example code showing object reference can’t be changed
public class PassValue { private int num; PassValue(int num){ this.num = num; } public static void main(String[] args) { PassValue pv = new PassValue(7); System.out.println("Value of num before method call " + pv.getNum()); pv.displayValue(pv); System.out.println("Value of num after method call " + pv.getNum()); } private void displayValue(PassValue obj){ // creating new object means reference is changed obj = new PassValue(10); System.out.println("Changed value in method " + obj.getNum()); } public int getNum() { return num; } public void setNum(int num) { this.num = num; } }Output
Value of num before method call 7 Changed value in method 10 Value of num after method call 7
Here you can see that a new object is created and assigned to the obj parameter in the displayValue() method. But that doesn’t change the pv object reference which shows even for objects Java is pass by value.
That's all for the topic Java Pass by Value or Pass by Reference. If something is missing or you have something to share about the topic please write a comment.
You may also like
No comments:
Post a Comment