Pass-by-reference vs Pass-by-value And Memory Relation In Java

Beran Santur
3 min readOct 20, 2021

--

What do passing by reference and passing by value mean? What are their differences? Let’s figure it out.

First of all, what is pass referred to?

“Call” means invoke a function, i.e.

myFavoriteFunction(); .

You “pass” variables into functions.

myFavoriteFunction(a).

In this example, a was passed into myFavoriteFunction.

Let’s start with the memory relation.

Primitive types only store one value for instance:

int i = 15;

above the appropriate amount of space is allocated with the given data type. And the variable’s value is stored in stack memory as it is.

But when it comes to objects things start to change. Since they are more complex they must be stored differently. They often hold one than one value, each of which must be stored in the memory. And object variable must hold a reference to get these values. The reference represents the location where the object is stored. Let’s start off with simple definitions.

Stack memory

Stack memory holds primitive types and addresses (addresses that refer to the location where the object resides) of objects

Note: An object reference on the stack is only an address that refers to the place where the object resides in the heap.

Heap memory

This is the place where actual object values are stored. Note that object references always refer to object addresses in the heap memory.

Note: An object reference on the stack is only an address that refers to the place in heap memory where that object is kept.

Pass by value vs Pass by reference

Pass-by-value: Passing a variable by its value. Primitive types are passed by that way.

Pass-by-reference: Passing a variable by its actual address. Objects are passed that way because their reference gets passed.

Pass by value

Let’s see it with an example

public static void main(String[] args) {
int firstNumber = 20;

int secondNumber = firstNumber;

firstNumber=15;

System.out.println(secondNumber); //prints 20
}

As you can see the actual value of the second number is copied to the second number’s value. Thus when we change the first number’s value it doesn’t affect the second number’s value. Because its name is different and now it has a different memory address with the same value. We don’t actually pass the memory address.

Pass by reference

Let’s see with an example

public static void main(String[] args) {
Dog firstDog = new Dog();
firstDog.setName("Molly");

Dog secondDog = firstDog;
firstDog.setName("Lilly");

System.out.println(secondDog.getName()); //prints Lilly
}

with new Dog() a new Dog object is created and firstDog is a reference to that object’s memory address. So when we assign firstDog’s value to secondDog variable. We actually pass the memory address of firstDog’s object. So when we change something in firstDog it will also affect the secondDog.

Note: The actual “value” of any variable on the stack is the actual value for primitive types (int, float, double, etc) or the reference(memory address) for reference types.

Note2: Java is actually pass-by-value all the time but with reference types it intuitively pass-by-reference. Since it still copies the value of the reference variable it still counts as pass-by-value. We can think java intuitively pass-by-reference for all the objects.

--

--

Beran Santur
Beran Santur

Written by Beran Santur

Hello, I am Beran I am a software engineer student, and I want to share all my experiences primarily for myself and all the others who want to learn coding.