Java is pass by value or pass by reference?

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.

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:

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:

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.

Leave a Comment

Your email address will not be published. Required fields are marked *