Is java pass by value or pass by reference?

We have always wondered if the java language is pass-by-value or pass-by-reference. The common misperception among people is that java is pass-by-value for primitive data types and pass-by-reference for non-primitive data types. But the actual concept is that

 “JAVA IS PASS-BY-VALUE FOR ALL THE DATA TYPES”

Now, first, let us understand what is pass-by-value and pass-by-reference.

Pass-by-value:

When the parameters are pass-by-value, their copies and not the actual parameters are passed as arguments.

This means that the actual values of parameters are not modified when parameters are passed by value.

Pass-by-reference:

When the parameters are pass-by-reference, the actual references of the parameters are passed as arguments.

This means that the actual values of the parameters are modified when parameters are passed by reference.

 

Now, that we have understood both these terms, let us understand how is java pass-by-value.

First, we will understand the concept in primitive data types.

(1) Primitive Data Types:

In java, the primitive data typess are as follows:

  • byte
  • short
  • int
  • float
  • double
  • char
  • boolean

Let us see what happens when the primitive data types are passed as parameters:

public class Primitive 
{
    public static void main(String[] args) 
    {
        int num=10;
        System.out.println("The initial value of num is: "+num);
        change(num);
        System.out.println("The final value of num is: "+num);
    }
    public static void change(int num)
    {
        num=20;
    }
}

Output:

 

 

 

 

As we have seen that the value of num variable has not changed. It is because of the fact that the copy of num variable is passed as argument. When it is passed in change() method, the value of copy of num variable is changed to 20, but the original num variable has remain unchanged.

Now, let us understand it diagramatically.

Now, you must have understood that java is pass-by-value in case of primitive data types.

(2) Non-Primitive Data Types:

In java, the non-primitive data types are as follows:

  • String
  • Array
  • Class
  • Interface

Let us see what happens when the non-primitive data types are passed as parameters:

public class NonPrimitive 
{
    public static void main(String[] args) 
    {
        Demo d=new Demo();
        d.num=10;
        System.out.println("The initial value of num is: "+d.num);
        change(d);
        System.out.println("The final value of num is: "+d.num);
    }
    public static void change(Demo d)
    {
        d.num=20;
    }
}
class Demo
{
    public int num;
}

Output:

 

 

 

 

As we have seen that the value of num variable has changed now. Now, let us understand what has happened.

It is because of the fact that when we passed object reference in change() method, its copy is passed as argument. But the previous reference is deleted and now the actual value and its copy is referencing to the same object i.e. the new value. Thus, now the actual value has changed.

Now, let us understand it diagramatically.

Now, you must have understood that java is pass-by-value in case of non-primitive data types.

Conclusion:

We have thus concluded that in java, the copy of parameters are passed as arguments and not the actual parameters. Thus, java is pass-by-value for both primitive and non-primitive data types.