How to pass by reference in java?

First of all, what is pass by value and pass by reference?

In Pass by value, values of actual parameters are copied to function’s formal parameters and the two types of parameters are stored in different memory locations. So any changes made inside functions are not reflected in the actual parameters of the caller function.

Passing by reference means that both the actual and formal parameters refer to the same locations, so any changes made inside the function are actually reflected in the actual parameters of the caller function.

Java always uses pass by value but, there are a few ways to achieve pass by reference:

Return a value and update it:

This is a simple method in which changes are returned by the function to​ update the original memory address. As you can see below, changes made to the variable "num" in the update function are reflected in the main function too. Therefore pass by reference is achieved.

class Main{
  public static void main (String [] arguments)
  {
    int num = 1;
    // printing before update
    System.out.println("number before update= " + num);
    // update function returns a value.
    num = update(num);
    // printing after update
    System.out.println("number after update= " + num);
  } 
  // update function
    public static int update( int num ){ 
    // increments number.       
     num++;
     return num; 
    }
}

Output-

number before update= 1 
number after update= 2

Create a single element array:

In this, a single-element array is created and passed as a parameter to the function and the effect is visible in the original memory address.

public static void main (String [] args)
  {
    // single element array.
    int number[] = { 1 };

    // printing before update.
    System.out.println("number before update = " + number[0]);
    //update function.
    update(number);
    // printing after update.
    System.out.println("number after update= " + number[0]);
  } 
  // update function.
    public static void update( int number[] ){ 
    // increments the number       
     number[0]++;
    }

Output-

number before update= 1
number after update= 2 

This is a built-in Java class, create an AtomicInteger object by passing this integer as a parameter to its constructor, now whenever you need the integer, you have to get it through the object of the AtomicInteger. Hence the Integer has been passed by reference.

public static void setvalue(AtomicInteger x) {

  // setting new value
  // thus changing the actual value
  x.set(1000);
}

public static void main(String[] args) {

  // provides single-threaded environment
  AtomicInteger y = new AtomicInteger(10);

  // passed by reference
  setvalue(y);

  System.out.println(y);
}

Output-

1000

Making a public member variable in a class -

In this method, an object of a class passes in the function and updates the public member variable of that object; ​changes are visible in the original memory address.

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.

public class Example {
  int Num;
  void Example() {
    Num = 0;
  }

  static void update(Example obj) {
    obj.Num++;
  }

  public static void main(String[] args) {
    Example obj = new Example();

    System.out.println("Number value " + (obj.Num));

    update(obj);

    System.out.println("Updated Number value " +
      (obj.Num));
  }
}

Output-

Number value 0
Updated Number value 1
0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions