Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 25 Aug, 2021
Geek Week

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 runtime 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: 

  1. Must be at the top level or a member of an object or a companion object.
  2. Should start from a primitive data-type
  3. 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.

Example:

Kotlin






const val gfgName = "Geeksforgeeks" // goes well
val coursename = "Android" // goes well
   
const val gfgName = getgfgName() // not goes well
val 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.

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:

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, “.png,” 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.

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :