Val vs Var in Kotlin

·

5 min read

Hey, curious minds! 👋 Today, let’s explore the basics of Kotlin programming together. I’m here to guide you through the concepts of ‘val’ and ‘var.’ Imagine it’s as straightforward as picking your favourite snack! 🚀😎 Ready to make coding easy? Let’s dive into the world of Kotlin together!

Introduction👋

So in this article, I show you how Val & Var really work‌

Think of a variable (var) like a box that can hold different things. With a var, you can change what’s inside the box whenever you want. It’s like having a lunchbox that you can fill with different snacks every day.

Now, consider a value (val) as a special box that you can only fill once and can’t change afterwards. It’s like having a time capsule — you put something inside, seal it, and it stays that way forever. You can’t swap out what’s inside later on.

So, in programming, when you use “var” you’re making a box that you can update or change. When you use “val” you’re creating a box that stays the same once you’ve put something in it.

// var example
var snackBox = "Cookies" // You can change the snack inside
snackBox = "Chips" // Now it's a different snack

// val example
val timeCapsule = "Memories" // You can't change what's inside
// timeCapsule = "New Memories" // This line would cause an error, you can't change it
So, using "var" is like having a box you can swap items in and out of, while "val" is like sealing a box with something special inside that you can't change.

let’s break it down in simpler termsđŸ«Ą

So, in simpler terms, declaring a variable is like picking a name for a box*, saying what kind of things can go/put into the box, deciding if you want to use the box again, putting something inside, and making sure you’re using the right type of box for what you’re putting in.*

Val vs. Var

Imagine you have a box, and you label it with the name “x.” Inside the box, you can put something, let’s say a number. Now, there are two types of boxes in our coding world: one is like a permanent box, and the other is like a changeable box.

Permanent Box (val):

If you use a permanent box (we call it “val” in code), it means whatever you put inside it stays there forever. Once you put a number, like 5, inside the box, you can’t take it out or replace it with another number. It’s like writing something in pen — permanent and unchangeable.

Changeable Box (var):

On the other hand, if you use a changeable box (we call it “var” in code), it’s like using a pencil instead of a pen. You can put something in the box, like the number 6, and if you want, you can erase it and put a different number, like 10.

In the second example, when you say x = 10, it doesn't create a whole new box. It's like erasing what was in the box and putting a new value. But with the permanent box, you can't do that. Once you put something inside, it stays that way.

So, using val is like having a permanent label on a box, and using var is like having a changeable label that you can rewrite if needed.

Using var with Arrays:

When you use var to declare a variable for an array, it means you can change which array that variable refers to. For example:

var myArray = arrayOf(1, 2, 3)
myArray = arrayOf(4, 5)

Here, we first create an array and assign it to myArray. Later, we create a new array and assign it to myArray again. With var, the variable can point to different arrays(reference).

Using val with Arrays:

Now, if you use val instead of var

val myArray = arrayOf(1, 2, 3)
// This won't compile: myArray = arrayOf(4, 5)

With val, once you assign an array to the variable, you cannot change it to point to a different array. The attempt to do so (commented line) will result in a compilation error.

You Can Modify the Array with val:

Even though you can’t change the variable to point to a different array with val, you can still modify the contents of the array:

val myArray = arrayOf(1, 2, 3)
// This is allowed, as it modifies the existing array:
myArray[2] = 6

So, with val, the variable is "stuck" with the same array, but you're free to modify the elements inside that array.

FAQ’S

1.Question: What is the difference between val and var in Kotlin?

Answer: The main difference is in mutability. val(value) is used to declare immutable variables (constants), while var(variable) is used for mutable variables.

2. Question: Can I use val with a non-primitive type, like a class?

Answer: Yes, you can use val with non-primitive types, including classes. It means the reference to the object is immutable, but the object’s state can still change.

3. Question: Are val variables thread-safe?

Answer: Yes, val variables are thread-safe because their values cannot be changed once assigned.

4. Question: Is there a performance difference between using val and var?

Answer: In general, there might be a slight performance advantage when using val because the Kotlin compiler can optimise certain operations knowing that the value is immutable. However, the difference is usually negligible in most cases.

5. Question: Can I use val with collections like lists or maps?

Answer: Yes, you can use val with collections. It means the reference to the collection is immutable, but you can still modify the contents of the collection:

val myList = mutableListOf(1, 2, 3)
// This is allowed, as it modifies the existing list:
myList.add(4)

Conclusion:

  • A variable holds a reference to an object.

  • An object has state and behaviour. Its behaviour is exposed through its functions.

  • val means the variable points to the same array forever


  • var means the variable can point to a different array
.

Reference:

pdfiles.net/storage/Books/headfirst/head_fi..

Please check my another storyđŸ™đŸ»

Â