Kotlin Pass by Reference and Pass by Value Tutorial

In this section, we will learn what the pass by value and pass by reference means and how they work in Kotlin.

What is Pass by Reference and Pass by Value in Kotlin?

Pass by reference and pass by value tries to explain how a value from one variable is passed to another variable or a parameter. Basically, it wants to explain if a copy of a value is passed or just a reference to it?!

For example, let’s say we have the value 3 stored in a variable. If we assign the value of this variable to another variable, what will happen? Are we going to have two variables and each with its own number or there will be only one number and both variables are pointing to it?!

In short, Kotlin works on top of Pass by Reference. So let’s see what this means practically now:

Kotlin Pass by Reference

When creating a value and assign it to a variable, that value is actually stored in a place called Heap Memory and wrapped in something called object.

The memory location of this value (AKA object) then will be assigned to the target variable.

So now the variable is actually holding the address of the value and not the value itself!

Now, if there was another variable and we wanted to assign the value of the first variable to the second variable, only the memory address is now passed by to the second variable!

This means both variables are now pointing to the same memory location and, hence, the same value.

Now there’s a subtle note to remember here, but before we get into that, let’s go and create a simple program and then we will talk about this subtlety.

Example: passing by reference in Kotlin

fun main(){
    var name = "John"
    var secondName = name 

    println(name)
    println(secondName)

    println("***")

    secondName = "Jack"

    println(name)
    println(secondName)
}

Output:

John

John

***

John

Jack

Here we have two variables name and secondName. The name variable is assigned the value John. What happened here is that the value John is stored as an object in the Heap memory and just its address is passed to the name variable.

Now in the second line, we can see that the secondName variable is assigned the value of the name variable. This means now the Memory Address of the John value is assigned to the secondName variable as well.

So now both name and secondName are pointing to the same value in the Heap Memory.

That’s how we got the same value on the output stream when called both variables using the println function.

After that, we’ve changed the value secondName variable to something else!

This is where the subtlety exists! You might be thinking that we are changing the value John to something else, but that’s not how the system actually works.

What happened here is that for the secondName, a new value Jack is created and stored in the Heap Memory and then the memory address of this new value returned back and assigned to the secondName variable.

Basically, now only the name variable is pointing to the value John and the other variable got a new memory address and new value as a result.

As you can see, whenever you create a new value and assign that to a variable, the value itself is stored in a part of the Heap Memory and only its address will be assigned to the target variable. Basically, what we do here is that we’re replacing the value of a variable which is only a memory address with the memory address of another value!

Notes:

  • Whenever you pass the value of a variable to another variable or even parameter of a function, it’s the memory address of the actual object that’s being passed around. You’re not copying the actual object around!
  • Also, when you replace the value of a variable with a new one, the old value will still stay in the memory for a short time until a program that is called Garbage Collector comes and removes that entirely from the memory space.
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies