Val vs Var in Kotlin
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âŠ.