Home » Difference between const and val in kotlin

Difference between const and val in kotlin

  • by
Difference between const and val in kotlin

1. Overview

In this article, we will learn the difference between const and val in Kotlin. Both the val and const are used to declare read-only properties.

In Kotlin, you can declare variables as mutable, using the var keyword, or as read-only, using the val keyword.

To know the differences between val and var, refer to this article.

2. Difference between const and val in Kotlin

Constval
Syntax:
const val ERROR_CODE = "Err001"
Syntax:
val errorCode : String = "Err001"
Read only propertyRead only property
We must assign the value of the const property at compile-timeWe can assign a value at compile time or run time.
Cannot change the value of the variable at run-timeCannot assign more than once
Equivalent to static keyword in Java.
final static String ERROR_CODE = "Err001"
Similar to the final keyword in Java.
final String errorCode = "Err001"
It must be a top-level property or a member of an object declaration or a companion object.We can declare as top-level, local variables, class variables, a member of an object declaration, or a companion object,..
Difference between const and val in Kotlin

3. Kotlin const

If you know the value of a read-only property at compile-time, mark it as a compile-time constant using the const modifier. A const property must adhere to the following requirements:

  • It must be a top-level property or a member of an object declaration or a companion object.
  • It must be of type String or a primitive type
  • You cannot provide a custom getter

For example, the following ERROR_CODE is a const variable whose value Err001 we know at compile time itself.

const val ERROR_CODE: String = "Err001"

4. Kotlin val

You can use the keyword val to define read-only variables (immutable). You can assign a value only once.

4.1. Local variables

You can refer the variables that you declare within or inside a function block as Local variables. Now, let’s see to declare these local variables in Kotlin.

You can use the val to define read-only local variables.

For example, the following main method contains a, b, c and d local read-only variables.

fun main() {

    val a: Int = 1  // immediate assignment
    val b = 2   // `Int` type is inferred
    val c: Int  // Type required when no initializer is provided
    c = 3       // deferred assignment

    println("a = $a, b = $b, c = $c")
}

You can assign values only once and cannot reassign. If you attempt to reassign, you would get the below error:

fun main() {

    val c: Int = 3       

    c = 4 // "Val cannot be reassigned" error thrown here
    println("a = $a, b = $b, c = $c")
}

4.2. Top level variables

Top-level variable has global scope within the file. For example, the following var variable a is declared at top level and accessible from both the main and test methods.

var a: Int = 1  

fun main() {
    println("a = $a")
    test()
    
    println("a = $a")
}

fun test() {
    println("a = $a")
    
    a = 2
}

Similarly, you can define val variable as top level variable. However, you cannot reassign.

val a: Int = 1  

fun main() {
    println("a = $a")
    test()
}

fun test() {
    println("a = $a")
}

4.3. Class properties

You can declare properties in Kotlin classes either as read-only, using the val keyword.

For example, the following class Employee contains var mutable variables.

class Employee {
    var name: String = "TedBlob"
    var empId: String = "1001"
    var dept: String = "Web"
}

The full syntax of a read-only property declaration differs from a mutable one in two ways: it starts with val instead of var and does not allow a setter.

val initialized = 1 // has type Int and a default getter
var initialized = 1 // has type Int, default getter and setter

5. Conclusion

To sum up, we have learned the difference between const and val in Kotlin. You can find code samples in our GitHub repository.

Leave a Reply

Your email address will not be published. Required fields are marked *