Open In App

Different Ways to Achieve Pass By Reference in Java

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 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
    }
}

Output
15
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 program to show the call by reference support
 
#include <stdio.h>
 
// formal parameters
void 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;
}

Output
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 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);
    }
}

Output
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




// 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));
    }
}

Output
Number value 0
Updated Number value 1

2.  Passing collections or Single element array as parameter: 




// 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);
    }
}

Output
Number Value 0
Number Value 1
List [1, 2]
Updated List [2, 3]

3.  Update the Return value:  




// 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);
    }
}

Output
Number 3
Updated number 4

 


Article Tags :