In pass by value mechanism copy of the value is created and passed to method. Any change in value passed is not reflected on actual variable which is used to pass value. Another mechanism is pass by reference. In this mechanism reference is passed to method.So any change in values are reflected on actual variable which is used to pass value.Ok, but there is a catch, reference has a different meaning in java. In java, parameters are passed by value. Yes, pass by reference is not used for parameter passing in Java.
The concept of pass by value is easy to understand for primitive parameters. But little bit tricky for objects, when you don’t have real understanding of object and reference. Following paragraph is focused on this concept.
Mr.Raj wants to build a new office for his ad agency. He goes to the architect to get a office building plan. When he gets a plan ready he builds a new office according to the plan. Now Mr.Raj needs a way to tell all stakeholders about his new office hence, he prints 100 Business Cards each one of them have office address on them.
There are 3 things in above paragraph which can be related to Java concepts.
- Building plan can be thought of as a Class
- Office is Object
- 100 Business cards are nothing but references to the Office object.
There are 100 references that will lead to one object. Let’s paint this picture in Java Memory.
| memory |
:::::::::::::
::::office:::<################
::::::::::::: #
::::::::::::: #
::::::::::::: #
::::::::::::: #
::::card1::::--------------->#
::::card2::::--------------->#
::::card3::::--------------->#
::::::::::::: "3 references"
Object is created by using new operator in memory. card1 is reference to office object
Office card1 = new Office();
card2 gets a copy of card1 reference. You can say card2 is another reference to Office object.
Office card2 = card1;
So, In java parameters passing is done through pass by value only. For objects it may look like java applies pass by reference but it is wrong. While calling method java passes a copy of reference of an object. Hence if any change applied in method on reference, then that change will be reflected on object and even after returning from method that change will be present for another references. Because they are pointing to same object.
Remember, there is only one office but many cards which are pointing to the office. If you change the color of office walls everyone who is visiting office with different cards will see same color on wall. That’s it!
So whenever you say we are passing object as a parameter in Java, It is wrong. You should say you are passing reference of an object. And this reference of an object is passed by value.
Putting it all together in example
import java.awt.Point;
/**
* @author dinsaw
* This is a demo to understand pass by value in java
*/
public class PassByValueDemo {
/**
* @param args
*/
public static void main(String[] args) {
int x = 10; // test int variable
System.out.println("In main before change x = " + x);
change(x);
System.out.println("In main after change x = " + x);
Point point = new Point(10, 10); // object of point class
System.out.println("In main before change point = "+ point.toString());
change(point); // here we are passing copy of reference to change method
System.out.println("In main after change point = "+ point.toString());
//test for array. remember array is also object
int arrayOfInt[] = {1,2,3,4}; //array of int
System.out.println("In main before change arrayOfInt[0] = "+ arrayOfInt[0]);
change(arrayOfInt);
System.out.println("In main after change arrayOfInt[0] = "+ arrayOfInt[0]);
}
private static void change(int[] arrayOfInt) {
arrayOfInt[0] = 5;
System.out.println("In change arrayOfInt[0] = "+ arrayOfInt[0]);
}
private static void change(Point point) {
point.setLocation(5, 5);
System.out.println("In change point = "+ point.toString());
}
private static void change(int x) {
x = 5;
System.out.println("In change x = " + x);
}
}
Output
In main before change x = 10
In change x = 5
In main after change x = 10
In main before change point = java.awt.Point[x=10,y=10]
In change point = java.awt.Point[x=5,y=5]
In main after change point = java.awt.Point[x=5,y=5]
In main before change arrayOfInt[0] = 1
In change arrayOfInt[0] = 5
In main after change arrayOfInt[0] = 5