What’s the Difference Between “const” and “val” in Kotlin?

21 Apr, 2022
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.
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-typeNo 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.
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.
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.
Example:
Kotlin
const val gfgName = "Geeksforgeeks" // goes wellval coursename = "Android" // goes well  const val gfgName = getgfgName() // not goes wellval coursename = getcoursename() // not goes well
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 getgfgame 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.
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:
Kotlin
GeeksforGeeksClass {    companion object {           const val IMAGE_EXTENSION = ".jpg"           val LOGONAME: String        get() = "Geeks" + System.currentTimeMillis() + IMAGE_EXTENSION    }}
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. 
Kotlin
public final String getIMAGENAME() {   return "Geeks" + System.currentTimeMillis() + ".jpg";}
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 -:
syntax -:
var variable_name= value
divyanshverma010
manojcoursera
mayank007rawa
Picked
Android
Difference Between
Kotlin
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Android SDK and it's Components
Flutter - Custom Bottom Navigation Bar
Retrofit with Kotlin Coroutine in Android
How to Post Data to API using Retrofit in Android?
Flutter - Stack Widget
Class method vs Static method in Python
Difference between BFS and DFS
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM