What is the difference between const and val in Kotlin?

Tram Ho

In every programming language, there are mutable properties and immutable properties. In Kotlin, there are two keywords for declaring an immutable property, const and val . Those who just started mobile programming in the Kotlin language will probably be a little confused as to when to use them. It seems so simple, but sometimes in job interviews are still mentioned by employers. So today we are going to look at the difference between const and val in Koltin to get a better understanding of it.

1. Const in Kotlin:

We use the keyword const to declare properties that do not change. These properties only allow us to read.

These properties must be assigned or initialized at compile-time to make it easier to understand when we are coding and that is why const is called a constant at. compile time. So, at runtime, no new value can replace the value of the const variable.

A const property has the following characteristics:

  • Must be placed on top-level either a member of an Object or a member of a companion object.
  • Must be initialized with String or primitive type.
  • Cannot customize the getter method again.

Therefore, we cannot assign a const variable to a function or a class, because in this case our variable will be initialized at runtime (the application runs) and not in time. translate.

For example: const val TAG = "NameFragment"

2. Val in Kotkin:

The common point of val and const is that both are used to declare readonly properties. But the biggest difference between them is that the val attribute can be initialized at application run time. So we can assign a val variable to any function or class.

For example: val name = "dongbin05"

At the end of the theory we move on to the example section to apply the knowledge of the theory section to better understand it.

3. Learn an example:

In the above example, we use cityName as an immutable variable. When using const, we have to assign a value directly to it, but if we try to assign a value to it as a getCityName function, the compiler will give an error because the variable will be assigned a value at runtime, not. at compile-time. With val, both of the above are true.

4. Why use const when we can use val?

In the example above, we see the val variable is initialized at runtime and const at compile time. So why would we use const if we could use val?

Let’s find out a specific case with the real example below:

In the example above, in the companion object we have declared a const variable named FILE_EXTENSION and a val variable named FILENAME with a custom getter method.

For every file, it has the extension “.png” so we declare the extension is a const. But each file must be distinguished by different names, specifically here we rely on the current system time. So the value of the file will be set at runtime so here we use val.

What happens after the compilation process is that wherever const is used, those variables will be replaced by their values. In the case of val, these variables are preserved because we don’t know the value of the val variable at compile time.

If you decode the above code into Java you will see:

Here the variable FILE_EXTENSION has been changed by its value “.png” and therefore we do not have to spend any money to access that variable during runtime. These are the benefits of using const versus val.

Hopefully through this article, you will understand the two keywords const and val in Kotlin. Thank you for following this article. If you have any questions or suggestions, please leave a comment below.

Reference: https://blog.mindorks.com/what-is-the-difference-between-const-and-val

Chia sẻ bài viết ngay

Nguồn bài viết : Viblo