Home » Difference between var and val in Kotlin

Difference between var and val in Kotlin

Difference between var and val in Kotlin

1. Overview

In this article, we will learn the difference between var and val in Kotlin. To learn more about Kotlin topics, refer to our articles.

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

1.1. Difference between val and var in Kotlin

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

If you want to reassign values to your variables (mutable), use the var keyword.

2. 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.

2.1. Kotlin val local variables

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")
}

2.2. Kotlin var local variables

You can use the var keyword to declare local variables that can accept reassignment of values.

For example, the following main method contains local variables for which you can assign values more than once. As you can see, we assigned values to c variable more than once.

fun main() {

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

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

3. 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. Class properties

You can declare properties in Kotlin classes either as mutable, using the var keyword, or 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 val and var in Kotlin. You can find code samples in our GitHub repository.

1 thought on “Difference between var and val in Kotlin”

  1. Pingback: Difference between const and val in kotlin - TedBlob

Leave a Reply

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