What is the difference between var and val in Kotlin?

In your code result is not changing, its var properties are changing. Refer comments below:

fun copyAddress(address: Address): Address {
    val result = Address() // result is read only
    result.name = address.name // but not their properties.
    result.street = address.street
    // ...
    return result
}

val is same as the final modifier in java. As you should probably know that we can not assign to a final variable again but can change its properties.

Leave a Comment