PASS BY REFERENCE IN JAVA

Java Method is invoked in two ways : Pass by Reference and Pass by value. They are also referred to as Call by Reference and call by value. They depict how the parameters are being passed to the methods.

Table of Contents

[hide]
  • What is Pass by Reference in java
  • Example :Pass by reference in java
  • What is Pass by value in java
  • Example :Pass by value in java
  • Example :Swapping of two integers
  • Is Java Pass by value or Pass by Reference

What is Pass by Reference in java

Pass by Reference in Java is a mechanism through which the original values of the parameters are changed. When passing actual parameters to the method call, the separate copy of the variables are not created, intead the reference of the original parameter is passed to the calling method.

To be more precise, pass by reference in Java does not create its own copy of original values, rather it refers to the original values which only differs by name i.e the references.

In Java all the reference types like objects, array are passed as reference while all primitive types are passed by value.

Example : Assume you're a freelancer and you work for a client. So as needed, client already has a live website, you just have to change some pre-existing features. So you'll write some code to modify certain files in the original codebase or repository. The changes would affect in the original files.

Pass by reference is mostly used in situations where the values of the original variables are to be changed using a method. Now let us see an example how pass by reference in Java works.

Example : Pass by reference in java

copyimage
//Java Program to demonstrate Pass By Reference in Java
public class Codebator{
    static int x=10;
    static int y=20;
       public static void main(String[] args) {
           System.out.println("Original value of x and y : " +x+ " "+ y);
           Codebator object = new Codebator();
           passbyreference(object);
           System.out.println("Change in the Original copy : " +x+ " "+ y);
	}
	public static void passbyreference(Codebator obj) {
	   obj.x=60;
	   obj.y=70;
	}
}

OUTPUT :

Original value of x and y : 10 20Change in the Original copy : 60 70 

In the given example we are passing the reference x and y as an object to the passbyreference method call. The given values of x and y are modified and thus the changes have been made in the original value.

What is Pass by value in java

Pass by Value in Java is a mechanism through which the original values of the parameters are not changed. When passing actual parameters to the method call, the separate copy of the variables are created, thus any change made in the the seperate copy does not effect the original values.

The benefit of this approach is that you cannot change the variables that are used to call the method because any change that occurs inside the method in on method copy of the arguments value. The original value of the arguments will remain intact.

Example : Let's take the same example as before, now the specifications have changed, the client has given you a prototype of his previous working website and asked you to create a new web page similar to that one. Now you start from scratch and create a web page from the reference template. Changes made will not impact the original design.

In Java all primitive types are passed by value.

Example : Pass by value in java

copyimage
//Java Program to demonstrate Pass By Value in Java
public class Codebator{
   public static void main(String[] args) {
       int original=10;
       System.out.println("The Original value is : " +original);
       passbyvalue(original);
       System.out.println("Value after method call : " +original);
  }
   public static int passbyvalue(int x) {
       x=50;
       System.out.println("Inside passbyvalue method : " +x);
       return x;
    }
}

OUTPUT :

The Original value is : 10Inside passbyvalue method : 50Value after method call : 10

In the given program the value of argument passed is 10. Inside the method call when x=50; takes place, the value of x is changed but not the value of variable original. Hence the ouput is retained and the original value is not affected.

Any change in the formal parameter is not reflected in the actual parameter in Java Pass by value, whereas in Java pass by reference any change in the formal parameter is reflected to the actual parameter.

Example : Swapping of two integers

copyimage
public class Codebator{
  public static int value1 = 50;
  public static int value2 = 100;
     public static void main(String[] args) {
        System.out.println("Initial Values "+ value1+ " "+ value2);

        passbyvalue(value1, value2);
        System.out.println("Final values : passbyvalue - "+ value1+" "+value2);
		       
        Codebator obj = new Codebator();
        obj.passbyreference(obj);

        System.out.println("Final values : passbyreference - "+ value1+" "+value2);
     }
      //pass by value
     public static void passbyvalue(int x, int y) {
        int result;
        result = x;
        x = y;
        y = result;
     }
      //pass by reference
      public static void passbyreference(Codebator obj) {
        int result;
        result = obj.value1;
        obj.value1 = obj.value2;
        obj.value2 = result;
     }
}

OUTPUT :

Initial Values 50 100Final values : passbyvalue - 50 100Final values : passbyreference - 100 50

Is Java Pass by value or Pass by Reference

According to the Java, it is said that everything is "strictly Pass by value". in Java values and references are the fundamental concepts. Primitive variables store the actual values of the parameters, while Non-Primitives store the reference variables that point to the addresses of the objects to which they refer. Both values and references are stored in the stack memory.

During the method execution, a copy of each parameters irrespective of its a value or reference is created in stack memory which is then passed to the method.

Primitive type values are copied inside the stack memory which is the passed to the method call while in non-primitive types the reference in stack memory points to the actual data which resides in the heap.

Conclusion : For primitive types the parameters are pass by value while for objects, the object reference is pass by value.