In this article we will learn about java is pass by value or pass by reference. It is very hard to say that unless we have deep understanding about pass by value and pass by reference.
Pass by value: In pass by value when we are passing parameters to method they will be copied to other variables. Those copied variables will be the arguments of the method. So whatever modifications done by the method on variables will be restricted to dummy variables only. It won’t affect the original variables.
int add(a,b) //a,b are arguments { a=a+b; return a; } //method calling k=add(c,d);
c, d are actual parameters of that we are passing. But add method will receive copy of the values of c, d as formal parameters. Now add will do a+b and assigns the value to ‘a’. But ‘c’ value won’t be changed. We didn’t sent any reference to the method that’s why value of ‘c’ is not changed.
Pass by reference: In Pass by reference when we are passing the parameters to the method the reference of actual parameters will be passed to formal parameters. So whatever changes made in method body will affect the actual parameters.
Java is always pass by value we can say. Let us see one example then we will discuss how it is pass by value.
Program for showing pass by value:
public class PassByValue { static void swap(int var1,int var2) { int temp; temp = var1; var1 = var2; var2 = temp; } public static void main(String args[]) { int value1 = 10; int value2 = 20; System.out.println("value1="+value1+"\nvalue2="+value2); swap(value1,value2); System.out.println("after changing values by pass by value"); System.out.println("value1="+value1+"\nvalue2="+value2); } }
Output:
value1=10
value2=20
after changing values by pass by value
value1=10
value2=20
If we observe the output we can clearly say that it is pass by value because swap function didn’t affect the actual parameters. Here actual parameters are value1 and value2 and formal parameters are var1, var2.
Whatever the values we entered those values will assigned to value1 and value2. Now these values copied into formal parameters not the reference of variables. That’s why after swap function also values of value1 and value2 didn’t change.
But confusion comes here when we pass object as a parameter. In java object means reference. This is basic for understanding this concept. Java object means memory reference.
When a method call needs to pass an object as a parameter java will pass that object by value only. If method does any operations on object values it will be modified. Based on this thing most of the people will say that java is pass by reference. But what happens in real is, when we are passing object as a parameter we are indirectly passing memory reference. Here slight difference is there between object passing and pass by reference when we observe clearly. Pass by reference means we will pass the variables with the memory references. Here we are passing object by value but object means memory reference because of that only original or actual variable values are changing.
Let us see Example for object passing:
class Example { int value1; int value2; void print() { System.out.println ("value1 = "+ value1 +"\nvalue2 = "+ value2); } } public class PassByValue { static void swap(Example object) // taking Example class object as parameter and swapping the values of the object { int temp; temp = object.value1; object.value1 = object.value2; object.value2 = temp; } public static void main(String args[]) { Example ob1 = new Example(); // creating object for Example class ob1.value1 = 5; ob1.value2 = 10; System.out.println("before swap"); ob1.print(); System.out.println("\nafter swap"); swap(ob1); ob1.print(); } }
Output:
before swap
value1 = 5
value2 = 10
after swap
value1 = 10
value2 = 5
Here we are passing a copy of the reference and hence it’s pass by value.
We can say that java is pass by value. Java variables are two types primitives and class objects. Whatever the variable are passing java will send as pass by value only. If those variables are primitives then changes in method won’t be reflected on actual variables. If those variables are objects then changes will be reflected.