What is the difference between pass by value and pass by reference in Java?

Many programmers often confuse between Java being Passed By Value or Pass By Reference. If only we could visualize our code, this won’t seem too big a problem.

Let’s start with the basics.

Data is shared between functions by passing parameters. Now, there are 2 ways of passing parameters:

  1. Pass By Value: The pass by value method copies the value of actual parameters. The called function creates its own copy of argument values and then uses them. Since the work is done on a copy, the original parameter does not see the changes.
  2. Pass By Reference: The pass by reference method passes the parameters as a reference(address) of the original variable. The called function does not create its own copy, rather, it refers to the original values only. Hence, the changes made in the called function will be reflected in the original parameter as well.

Java follows the following rules in storing variables:

  1. Local variables like primitives and object references are created on Stack memory.
  2. Objects are created on Heap memory.

Now coming to the main question: Is Java Pass by Value or Pass by Reference?

Java Always follows Pass by Value

Let’s see an example to understand the same:

Primitive Pass By Value Example

Explanation:

As java follows pass by value mechanism, the processData function worked on the copy of data. Hence, no change in data was there in the original calling function

Let's see another example:

Object Passing

What happened up there? If Java is passed by Value then why did my original List got updated? 😮 Looks like java is not passed by value after all? 🤔Wrong. Repeat after me

Java Always follows Pass by Value

Explanation:

Consider the following diagram to understand it better.

Stack & Heap Memory Representation

In the above program, “fruits” is passed to the processData function. “fruitsRef” is a copy of the “fruits” param. Both fruits and fruitsRef are created on Stack. They are two different references. But the interesting point is, it points to the same underlying object in Heap. So any change that you make using one reference is going to impact the common object.

Let’s see one more example:

Object Pass By Reference

Explanation:

Stack & Heap Memory Representation

In this case, we used the “new” operator to change the reference of the fruitsRef variable. fruitsRef now points to a new object and hence any change that you make in this object is not going to impact the original fruits list object.

In conclusion, Java always follows Pass by Value. However, we should be careful while passing object references between methods.

The above concept is very important to correctly implement your problem statements.

For Example: Let's consider a program to delete a node at a given position in a Single Linked List.

Linked List Deletion of Specific Node

Solution:

class Node {
int data;
Node next;
Node(int d){
data = d;
next = null;
}
}
class LinkedList { public static Node push(Node head, int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
return head;
}
public static void deleteNode(Node head, int position) {

// List is empty
if (head == null){
return;
}

// If position is 1st, removing head node
if (position == 1) {
head = head.next;
return;
}
Node prevNode = head;
int i = 2;
while (prevNode != null && i != position) {
prevNode = prevNode.next;
i++;
}
// When position is more than number of node
if (prevNode == null || prevNode.next == null) {
return;
}
prevNode.next = prevNode.next.next;
}
public static void printList(Node head) {
Node currNode = head;
while (currNode != null) {
System.out.print(currNode.data + " ");
currNode = currNode.next;
}
}
public static void main(String[] args) {
Node head = null;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
System.out.println("Created Linked list is: ");
printList(head);

// Delete node at position 2
deleteNode(head, 2);

System.out.println("\nLinked List after Deletion at position 2: ");
printList(head);
}
}

The above solution works in all test cases except one. When you delete the first node i.e. Position=1. Based on the previous section concept, can you now understand why it doesn’t work? Perhaps the following diagram will be of some help 🤓

Deleting the First Position Node of a Single Linked List

To correct the above algorithm, we need to do the following:

public static Node deleteNode(Node head, int position) {
// List is empty
if (head == null){
return head;
}

// If position is 1st, removing head node
if (position == 1) {
head = head.next;
return head;
}
Node prevNode = head;
int i = 2;
while (prevNode != null && i != position) {
prevNode = prevNode.next;
i++;
}
// When position is more than number of node
if (prevNode == null || prevNode.next == null) {
return head;
}
prevNode.next = prevNode.next.next;
return head;
}
public static void main(String[] args) {
Node head = null;
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
System.out.println("Created Linked list is: ");
printList(head);

// Delete node at position 2
head = deleteNode(head, 2);

System.out.println("\nLinked List after Deletion at position 2: ");
printList(head);
}
//Rest of the code remains same

In this article, we discussed one small yet significant concept of Java: Parameter Passing. Keep watching this space for more content. Happy reading.

What is the difference between pass by value and pass by reference?

Definition. Pass by value refers to a mechanism of copying the function parameter value to another variable while the pass by reference refers to a mechanism of passing the actual parameters to the function. Thus, this is the main difference between pass by value and pass by reference.

What is pass by value and pass by reference in Java with example?

Pass by Value: It is a process in which the function parameter values are copied to another variable and instead this object copied is passed. This is known as call by Value. Pass by Reference: It is a process in which the actual copy of reference is passed to the function. This is called by Reference.

What does pass by reference mean in Java?

A mutable object's value can be changed when it is passed to a method. An immutable object's value cannot be changed, even if it is passed a new value. “Passing by value” refers to passing a copy of the value. “Passing by reference” refers to passing the real reference of the variable in memory.

What is the difference between pass by reference and pass by address?

Pass by reference (also called pass by address) means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory, i.e. the caller and the callee use the same variable for the parameter.