Different Ways to Achieve Pass By Reference in Java


The most frequent query asked by beginner programmers, especially those who have already worked with C and C++ in the past, is whether Java supports pass-by-referenceor pass-by-value. Generally, programming languages use pass-by-value and pass-byreferencefor passing parameters to a method. However, Java does not support both approaches rather it uses pass-by-value to pass both primitive and reference type values. But, it provides a few ways to achieve pass-by-reference, we will explore those through this article.

Ways to Achieve Paas By Reference

Let’s start this discussion by understanding the storage mechanism of Java. The reference variables, names of methods and classes are stored in stack and their values are storedin heap. But, the primitives are stored directly in the stack memory along with their values.

As explained earlier, Java supports only pass-by-value for both primitive and reference types which means when a method is invoked, a copy of the value of each parameter is passed to that method.

For primitive types such as int, double and Boolean the value of the parameter is sameas the original value of the variable. For example, if we have a variable ‘x’ with a value of10, and we pass ‘x’ as a parameter to a method, then the method receives a copy oforiginal value 10 as its parameter.

Approach

  • Define a user-defined method named ‘changeVal()’ to modify primitive type.

  • In the main() method, declare and initialize an integer type variable.

  • Now, call the user-defined method to perform operation and exit.

Example 1

The following example shows pass by value in Java.

import java.util.*;
public class ParameterPassing {
   public static void changeVal(int n) {
      n = n * 2; // modifying value
      System.out.println("Inside method: n = " + n);
   }
   public static void main(String[] args) {
      int val1 = 10;
      System.out.println("Before calling changeVal method: val1 = " + val1);
      changeVal(val1); // calling the method
      System.out.println("After calling changeVal method: val1 = " + val1);
   }
}

Output

Before calling changeVal method: val1 = 10
Inside method: n = 20
After calling changeVal method: val1 = 10

As you can see in the output, for primitive type ‘val1’, its value does not change aftercalling ‘changeVal’ method.

Pass By Reference handled using Pass By Value

Since reference variables are stored in stack, therefore, for reference types such as arrays, objects and strings the value of the parameter is the reference or address of the given variable. For example, if we have an array ‘arr’ with elements {1, 2, 3} and we pass ‘arr’ as a parameter to a method, then the method receives the copy of reference or address of ‘arr’ as its parameter.

Approach

  • Define a user-defined method named ‘changeAray()’ to modify reference type.

  • In the main() method, declare a reference type variable.

  • Now, call the user-defined method to perform operation and exit.

Example 2

The following example demonstrates pass by reference.

import java.util.*;
public class ParameterPassing {
   public static void changeAray(int[] a) {
      a[0] = a[0] * 2; // modifying value
      System.out.println("Inside method: a = " + Arrays.toString(a));
   }
   public static void main(String[] args) {
      int[] aray = {1, 2, 3};
      System.out.println("Before calling changeAray method: arr = " + Arrays.toString(aray));
      changeAray(aray); // calling the method
      System.out.println("After calling changeAray method: arr = " + Arrays.toString(aray));
   }
}

Output

Before calling changeAray method: arr = [1, 2, 3]
Inside method: a = [2, 2, 3]
After calling changeAray method: arr = [2, 2, 3]

Above output show that for reference type ‘arr’, its value does change after calling ‘changeAray’ method.

Store modified value back to the Original Variable

This approach will explain how we can achieve pass by reference of primitive variables. We can do this by storing the modified value back to the original value. First, we willdeclare and initialize a variable. Then, define a return-type method to modify its value. Inthe end, call the method and store its return value back to the original variable. This will result in a successful change in value.

Example 3

The following example demonstrates the above approach.

public class ParameterPassing {
   public static int changeVal(int nums) {
      return nums = nums * 2; // modifying value
   }
   public static void main(String[] args) {
      int val1 = 10;
      System.out.println("Before calling changeVal method: val1 = " + val1);
      int newNums = changeVal(val1); // calling the method
      val1 = newNums; // storing modified value
      System.out.println("After calling changeVal method: val1 = " + val1);
   }
}

Output

Before calling changeVal method: val1 = 10
After calling changeVal method: val1 = 20

Conclusion

Passing reference type value as a parameter works differently than primitive types. In this article, we learned different ways to achieve pass-by-reference of both primitives and reference type variables through example programs.

Updated on: 21-Jul-2023

338 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements