Kotlin mutable & immutable variable – Val vs Var

In the tutorial, JavaSampleApproach explains the differences between Kotlin Val vs Var.

1. Kotlin Val

val is used to declare a read-only variable. It likes final in Java.
So we can NOT re-assign a new value for val variable.

kotlin val vs var - val

2. Kotlin Var

var is used to declare a mutable variable. So we can change value of var variables.

3. Kotlin Val & Var with Object’s Properties

With Object’ properties, val and var do as the same above rule. We can change value for var properties of objects, but can NOT change value for val properties of objects.

-> Program runs normally when we change name value of val or var Customer’s objects:

But we will get errors when change value val id property:

kotlin val vs var - problems

By grokonez | December 13, 2017.

Related Posts

Got Something To Say:

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