What’s the Difference Between “const” and “val” in Kotlin?
In Kotlin, as in other programming languages, properties can be mutable (changeable) or immutable (not changeable). To declare an immutable property, we utilize the terms const and val. Developers are frequently perplexed as to when to utilize one of these phrases. In this article, we will examine the distinction between const and val in Kotlin.
Use of “const” in Kotlin
The const keyword is used to declare properties that are immutable in nature, i.e. read-only properties. The values are only known on the compile-time though, as a result, no values may be assigned at runtime to const variables. To be a const property, a property must meet the following requirements:
- Must be at the top level or a member of an object or a companion object.
- Should start from a primitive data-type
- No custom getter So, you can’t assign a const variable to a function or a class because the variable will be initialized at runtime rather than compile-time.
Use of “val” in Kotlin
The primary distinction between const and val is that val properties can also be initialized at runtime. As a result, you may assign a val variable to a method or a class.
The immutable variable in the above example is gfgName. When using const, directly assigning the value is OK; however, attempting to assign the value from some function getgfgname will result in an error since the value will be assigned at runtime rather than compile-time. However, in the case of val, both scenarios are acceptable.
Why use “const” when “val” will suffice?
We saw in the last example that the val variable’s value is initialized at runtime and const at compile time. So, why use const when val will suffice? Let’s look at another real-world Android example to see how const and val are used:
GeekTip: In the above example, we declare the const value IMAGE_EXTENSION in the companion object and initialise the FILENAME variable as val using a custom getter.
Because the file extension will always be the same, it is defined as a const variable. However, the file name will be modified based on the rationale we apply for the file name. In this example, we’re naming the file after the current time. You can’t give it a value at first since the value is obtained during runtime. So we’re going to use val here.
In the above example, we declare the const value FILE EXTENSION in the companion object and initialize the FILENAME variable as val using a custom getter. Because the file extension will always be the same, it is defined as a const variable. However, the file name will be modified based on the rationale we apply for the file name. In this example, we’re naming the file after the current time. You can’t give it a value at first since the value is obtained during runtime. So we’re going to use val here.
Here, the variable IMAGE_EXTENSION has been replaced by its value, “.jpg,” indicating that the value has been inlined and therefore there is no overhead to access that variable at runtime. This is the benefit of using const instead of val.
Let us see the differences in a tabular form -:
|1.||It is a keyword that is used when we want a variable value to remain constant.||It is a keyword that is used to declare a variable in kotlin.|
|2.||It is only used to declare a read-only property of a class in kotlin.||It is initialized at runtime.|
|3.||Its value is known at compile time of the program.||It cannot be assigned multiple times.|
|4.||We cannot change the value of the variable that is declared constant.||It can be used with the const keyword.|
|5.||It is used at the start of the variable declaration.|
var variable_name= value
Please Login to comment...