What is the difference between "const" and "val" in Kotlin?

AndroidMobile DevelopmentApps/ApplicationsKotlin

const Keyword

The const keyword is used in Kotlin whenever the variable value remains const throughout the lifecycle of an application. It means that const is applied only on immutable properties of the class. In simple words, use const to declare a read-only property of a class.

There are some constraints that are applied to the const variable. They are as follows −

  • const can only be applied to the immutable property of a class.

  • It cannot be assigned to any function or any class constructor. It should be assigned with a primitive data type or String.

  • The const variable will be initialized at compile-time.

Example

In the following example, we will declare a const variable and we will use the same variable in our application.

const val sName = "tutorialspoint";
// This line will throw an error as we cannot
// use Const with any function call.
// const val myFun = MyFunc();

fun main() {
   println("Example of Const-Val--->"+sName);
}

Output

It will yield the following output −

Example of Const-Val--->tutorialspoint

Val Keyword

In Kotlin, val is also used for declaring a variable. Both "val" and "const val" are used for declaring read-only properties of a class. The variables declared as const are initialized at the runtime.

  • val deals with the immutable property of a class, that is, only read-only variables can be declared using val.

  • val is initialized at the runtime.

  • For val, the contents can be muted, whereas for const val, the contents cannot be muted.

Example

We will modify the previous example in order to pass a function using val and we won't get any errors at runtime.

const val sName = "tutorialspoint";

// We can pass function using val
val myfun=MyFunc();

fun main() {
   println("Example of Const-Val--->"+sName);
   println("Example of Val--->"+myfun);
}

fun MyFunc(): String {
   return "Hello Kotlin"
}

Output

Once you execute the code, it will generate the following output −

Example of Const-Val--->tutorialspoint
Example of Val--->Hello Kotlin
raja
Updated on 27-Oct-2021 08:54:22

Advertisements