Java Pass by Value vs Pass by Reference in Detail

In this tutorial article we will understand the 2 important concepts of pass by value and pass by reference in java programming and study its working and differences.

First of all we should understand what is meant by pass by value or pass by reference.

  • Pass by Value: The method parameter values are copied to another variable and then the copied variable is passed, that’s why it’s called pass by value.
  • Pass by Reference: An alias or reference to the actual parameter is passed to the method, that’s why it’s called pass by reference.

Important point to remember –

  • In Java, all primitives like int, char, etc are similar to C/C++, but all non-primitives (or objects of any class) are always references. So it gets tricky when we pass object references to methods. Java creates a copy of references and pass it to method, but they still point to same memory reference.
  • Thus all primitive datatypes are always passed by value by default where as non-primitive datatypes are passed by reference.

The best way to understand the working of these 2 methodologies is to study example programs and to see what happens behind the scenes in the actual memory –

Pass by Value –

Lets consider an example of java pass by value and we will understand what happens in the memory.

Before the function a: 5
Inside Display Primitive method
a :10
After the function a: 5

So as you can see from the output of the program, even when we passed the variable a in the displayPrimitive(a) method, since it was passed by value, there was another integer variable created inside the displayPrimitive(a) and the value 5 was copied into it. Thus the original value of the variable a which was declared in main() method was not changed.

Lets try to see this in terms of the memory diagram –

pass by value in java working

So as you can see from the memory diagram, there are 2 memory blocks for the 2 methods – main() method and displayPrimitive() method. Inside each block we have 2 individual integer variables named – “a”. So when the method displayPrimitive() is called in the main() method, the value of integer a(of the main method) is copied in a new int a variable(of the displayPrimitive method) and thus it is referred as pass by value. So there are 2 separate variables (one in the main method and one in displayPrimitive method). Hence the original variable int a in the main method is not affected when the displayPrimitive method is called.

Pass by Reference –

Lets consider an example of java pass by reference and we will understand what happens in the memory.

Before the method
2 2 2
Inside Display Array method
0 0 0
After the function
0 0 0

So in this java pass by reference program example, by looking at the output, you can say that the original array values (arr[]) have changed after it was passed by reference into the displayArray() method. So let us understand the working of java pass by reference through the memory diagram –

pass by reference in java - working

So as you can see the the diagram, initially in the main() method, an integer array named “arr” with size 3 is created. Since arrays in java are objects and not primitive types, they are stored in the heap memory as you can see in the diagram. So the reference variable arr does not store the actual values but points to the memory location in the heap memory where the actual array values are being stored. Now when the displayArray() method is called, a new reference variable named – “a” is created in the stack memory, however it still points to the same heap memory location where the original integer array named arr was created. Thus when the values are changed using the variable in the displayArray() method, by default the values are also changed for arr since both and arr point to the same memory location where the actual array values are being stored.
Thus from this you can understand the when we pass by reference, the original object values can also be changed in the method.

Leave a Reply

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