# Pass by value or pass by reference ?

In 
Published 2024-01-02

# Explanation

Generally speaking, the parameters could be passed to the called method in 2 ways:

  • Pass-by-reference: When a method is called, the method arguments reference the same variable in memory as the caller.
  • Pass-by-value: When a method is called, the caller passes a copy of the argument variables to the method (we will have 2 values/copies in memory).

# Example - passing by value

Fot the primitives and Wrapper Classes (Integer, String, etc) the behaviour under the hood is as for passing by value. We have below a short example.

Let's consider a simple class named "MyClass".

package com.example.demo;

public class MyClass {
   public Integer myInteger;

    public MyClass(Integer myInteger) {
        this.myInteger = myInteger;
    }
    void multiplyBy(Integer val) {
        this.myInteger = this.myInteger * val;
    }
}

and a main class which use this class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

  public static void main(String[] args) throws InterruptedException {
    SpringApplication.run(Demo1Application.class, args);

    Integer myInteger1 = 2;
    MyClass myClass = new MyClass(myInteger1);

    myClass.multiplyBy(100);

    System.out.println("-------------------------");
    System.out.println("myInteger1= " + myInteger1);
    System.out.println("myClass.myInteger= " + myClass.myInteger);
    System.out.println("-------------------------");
  }
}

When we run the main class we will see:

-------------------------
myInteger1= 2
myClass.myInteger= 200
-------------------------

# Example - passing a Class as parameter (by reference)

Let's consider a simple class named "MyClass".

For classes
package com.example.demo;

public class MyClass {
    Car car;

    public MyClass(Car car) {
        this.car = car;
    }
    void modifyColor(String color) {
        car.setColor(color);
    }
}

and a main class which use this class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Demo1Application.class, args);

		System.out.println("-------------------------");

		Car car1 = new Car("green");
		System.out.println("car1 = " + car1);
		System.out.println("car1.color = " + car1.color);

		MyClass myClass = new MyClass(car1);
        myClass.modifyColor("yellow");

		System.out.println("-------------------------");
		System.out.println("car1 = " + car1);
		System.out.println("car1.color = " + car1.color);
		System.out.println("-------------------------");

    }
}

When we run the main class we will see:

-------------------------
car1 = com.example.demo.Car@70925b45
car1.color = green
-------------------------
car1 = com.example.demo.Car@70925b45
car1.color = yellow
-------------------------

# Example - passing an Array as parameter (by reference)

Fot the Arrays the behaviour is the same as for the Classes.

Let's consider a simple class named "MyClass" as below:

package com.example.demo;

public class MyClass {
    int[] myArray;

    public MyClass(int[] arr) {
        this.myArray = arr;
    }
    void modifyArray(int valueIn) {
        this.myArray[0] = valueIn;
    }
}

and a main class which use this class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo1Application {

  public static void main(String[] args) throws InterruptedException {
    SpringApplication.run(Demo1Application.class, args);

    System.out.println("-------------------------");

    int[] myArray1 = {10, -7, -8, 3, 2, 0, -1};

    System.out.println("myArray1 = " + myArray1);
    System.out.println("myArray1[0] = " + myArray1[0]);

    MyClass myClass = new MyClass(myArray1);
    myClass.modifyArray(200);

    System.out.println("-------------------------");
    System.out.println("myArray1 = " + myArray1);
    System.out.println("myArray1[0] = " + myArray1[0]);
    System.out.println("-------------------------");

  }
}

When we run the main class we will see:

-------------------------
myArray1 = [I@46cc127b
myArray1[0] = 10
-------------------------
myArray1 = [I@46cc127b
myArray1[0] = 200
-------------------------

# Example - passing a Collection as parameter (by reference)

Fot the Arrays the behaviour is the same as for the Classes.

Let's consider a simple class named "MyClass".

package com.example.demo;

import java.util.ArrayList;

public class MyClass {
    ArrayList<String> myList;

    public MyClass(ArrayList<String> myList) {
        this.myList = myList;
    }
    void addElement(String element) {
        this.myList.add(element);
    }
}

and a main class which use this class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;

@SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Demo1Application.class, args);

		System.out.println("-------------------------");

		ArrayList<String> carList = new ArrayList<>();
		
		carList.add("Audi");
		carList.add("Renault");

		System.out.println("carList = " + carList);
		System.out.println("carList.size = " + carList.size());

		MyClass myClass = new MyClass(carList);
		myClass.addElement("Ford");

		System.out.println("-------------------------");
		System.out.println("carList = " + carList);
		System.out.println("carList.size = " + carList.size());
		System.out.println("-------------------------");
    }
}

When we run the main class we will see:

-------------------------
carList = [Audi, Renault]
carList.size = 2
-------------------------
carList = [Audi, Renault, Ford]
carList.size = 3
-------------------------