Pass by Reference

This lesson explains the concept of pass by reference.

Question # 1

What is passing by reference?

Passing by reference doesn't apply to Java! Reference data type parameters, such as objects, are also passed into methods by value. This means that when the method returns, the passed-in reference still references the same object as before.

To understand the concept of passing by reference, we'll go through an example. Consider the below code snippet.

public class SuperList {

    // Constructor
    public SuperList(int n) {
1.       List<Integer> superList;
2.       allocate(superList, n);
    }

    // Method that does initialization
    void allocate(List<Integer> list, int n) {
3.       list = new ArrayList<>(n);
    }
}

What should happen when we initialize an object of class SuperList? It will be null, which may seem counterintuitive.

  • Consider superList to be a holder that will hold a value of null on line 1.

  • On line 2, we are passing a value of null and not the variable superList itself. This is a very important distinction to realize.

  • When program control, reaches line 3, the list variable is not the variable superList. In fact, it's a brand-new variable (holder) which receives a value of null.

  • Line 3 also initializes the list variable to an object of ArrayList and the list variable will hold the reference or the address of the ArrayList object in the memory(heap).

  • When the program control returns to line 2, superList is still null because it was never passed in and assigned the ArrayList object.

In Java, we are copying the reference or the address the reference data type variable holds and passing it, and not the actual variable.

Consider the below diagram for further clarity.

Note that objects are always created in heap memory and the program variables are only references or addresses to them. So, when we pass a reference data type, the address of the object in the heap memory is copied and passed along. The receiving method can use the reference or the address to manipulate the object in the heap.

import java.util.*;
class Demonstration {
public static void main( String args[] ) {
SuperList obj = new SuperList(5);
System.out.println("superList = " + obj.sList);
}
}
class SuperList {
public List<Integer> sList;
public SuperList(int n) {
List<Integer> superList = null;
allocate(superList, n);
sList = superList;
}
void allocate(List<Integer> list, int n) {
}
}

Question # 2

What will be the output of the run method for the IntegerSwap class below?

public class IntegerSwap {

    public void run() {
        Integer x = 5;
        Integer y = 9;
        System.out.println("Before Swap x: " + x + " y: " + y);
        swap(x, y);
        System.out.println("After Swap x: " + x + " y: " + y);
    }

    private void swap(Integer a, Integer b) {
        Integer temp = a;
        a = b;
        b = temp;
    }
}

The output for the two print statements will exactly be the same, i.e. there will be no swapping. Follow the diagram below to understand why.

class IntegerSwap {
public static void main( String args[] ) {
(new IntegerSwap()).run();
}
public void run() {
Integer x = 5;
Integer y = 9;
System.out.println("Before Swap x: " + x + " y: " + y);
swap(x, y);
System.out.println("After Swap x: " + x + " y: " + y);
}
private void swap(Integer a, Integer b) {
Integer temp = a;
a = b;
b = temp;
}
}

As you can see from the diagram, a and b appear as stack variables holding addresses of integer object locations. Once the program control returns back to the run method, the x and y keep pointing to the same integer objects in heap because they passed in the references or the addresses of the integer objects and not themselves.

Question # 3

What value will be printed from the following snippet?

        String[] students = new String[10];
        String studentName = "You are an awesome developer";
        students[0] = studentName;
        studentName = null;
        System.out.println(students[0]);
Q
A)

You are an awesome developer

B)

null

class Demonstration {
public static void main( String args[] ) {
String[] students = new String[10];
String studentName = "You are an awesome developer";
students[0] = studentName;
studentName = null;
System.out.println(students[0]);
}
}