Different Ways to Achieve Pass By Reference in Java - GeeksforGeeks

29 Sep, 2021
There are two types of parameters one is Formal parameters and the second is Actual Parameters. Formal parameters are those parameters that are defined during function definition and Actual parameters are those which are passed during the function call in other Function.
Showcasing the formal and actual parameters through code:
Java
// Java program to show the difference// between formal and actual parameters import java.io.*; class GFG {  static int sum(int a,int b) // Formal parameters  {    return a+b;  }    public static void main (String[] args) {       int a=5;       int b=10;             System.out.println(sum(a,b));  //This is actual parameters    }}
15
Functions can be differentiated on the basis of parameters.
And there are two ways to pass the parameters in functions: call by value and call by reference.
C/C++ supports the call by reference because in the call by reference we pass the address of actual parameters in the place of formal parameters using Pointers.
And Java does not support Pointers that’s why Java does not support Call by Reference but c/c++ support pointers hence these language support call by Reference.
Swapping of Two numbers in C/C++ language is done by the call by Reference approach
Below is the implementation of Call by Reference in C
C
// C program o show the call by reference support #include <stdio.h> // formal parametersvoid swap(int* x, int* y){     int temp;       // save the value at address x    temp = *x;       // put y into x    *x = *y;       // put temp into y    *y = temp;     return;}int main(){    int a = 15;    int b = 5;    printf("Value of a %d\n", a);    printf("Value of b %d\n", b);     // swapping the value using call by reference    swap(&a,&b);     printf("Value of a %d\n", a);     printf("Value of b %d", b);         return 0;}
Value of a 15
Value of b 5
Value of a 5
Value of b 15
In java, if we try to swap numbers in java the changes in the state of the two numbers is valid only in the particular swapping method 
Swap two numbers in java:
Java
// Java program to swap two numbers import java.io.*; class GFG {    static void swap(int a, int b)    {        int temp = a;         a = b;         b = temp;         System.out.println("Value of a in swap function "                           + a);         System.out.println("Value of b in swap function "                           + b);        return;    }     public static void main(String[] args)    {        int a = 5;        int b = 10;               // original value of a        System.out.println("Value of the a "                           + a);                 // original value of b        System.out.println("Value of the b "                           + b);         // swap the numbers        swap(a, b);         System.out.println("Value of the a " + a);         System.out.println("Value of the b " + b);    }}
Value of the a 5
Value of the b 10
Value of a in swap function 10
Value of b in swap function 5
Value of the a 5
Value of the b 10
There are some ways to achieve pass by reference in java in the place of the call by reference: 
1.  Make a particular variable of a particular datatype as a class member
A class member can be accessed throughout the class definition and we can access members using this keyword when there is a local variable that has the same name as the class member’s name present in the particular function body or function definition in order to avoid confusion.
In order to pass the reference, we pass the object of the class in the place of the actual parameter and the formal parameter of a class object type has the same reference to each other that’s why with the help of the formal parameter object of class any changes will be reflected in both objects formal and actual objects.
Java
// Java program to make a particular variable// of a particular datatype as a class member import java.io.*; class GFG {    int Number;    void GFG() { Number = 0; }     static void update(GFG ob) { ob.Number++; }     public static void main(String[] args)    {        GFG ob = new GFG();         System.out.println("Number value " + (ob.Number));         update(ob);         System.out.println("Updated Number value "                           + (ob.Number));    }}
Number value 0
Updated Number value 1
2.  Passing collections or Single element array as parameter: 
We can pass collections like ArrayList, stack, etc or Single element array as actual parameters, and both the actual and formal parameters of the function have the same reference to the memory address.
Java
// Java program to passing collections// as parameters import java.io.*;import java.util.*; class GFG {    static void update(int a[])    {        // formal parameter        a[0]++;    }     static void update2(ArrayList<Integer> A)    {        for (int i = 0; i < A.size(); ++i) {             // update the item of            // the arraylist            A.set(i, A.get(i) + 1);        }    }    public static void main(String[] args)    {        // using single array of element        int a[] = new int[1];         System.out.println("Number Value " + a[0]);         // actual parameter        update(a);         System.out.println("Number Value " + a[0]);         // using Arraylist collection        ArrayList<Integer> A = new ArrayList<>();         A.add(1);        A.add(2);         System.out.println("List " + A);         // actual parameter        update2(A);         System.out.println("Updated List " + A);    }}
Number Value 0
Number Value 1
List [1, 2]
Updated List [2, 3]
3.  Update the Return value:  
Naive Approach to perform reference by simply updating the return value with the previous value. 
Java
// Java program to show the return// value getting updated import java.io.*; class GFG {    static int update(int number)    {        // update the number        return ++number;    }     public static void main(String[] args)    {        int number = 3;         // printing the number        System.out.println("Number " + number);         int returnnumber = update(number);         // update the number        number = returnnumber;         // printing the updated number;        System.out.println("Updated number " + number);    }}
Number 3
Updated number 4
 
sooda367
Picked
Technical Scripter 2020
Java
Technical Scripter
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Internal Working of HashMap in Java
Introduction to Java
Comparator Interface in Java with Examples
PriorityQueue in Java