Difference between var and val in Kotlin
var and val are both used to declare variables in Kotlin language. However, there are some key differences between them:
It is a general variable. The value of a variable that is declared using var can be changed anytime throughout the program. var is also called mutable and non-final variable, as there value can be changed anytime.
Previous marks is 10 New marks 30
The object stored using val cannot be changed, it cannot be reassigned, it is just like the final keyword in java. val is immutable. Once assigned the val becomes read-only, however, the properties of a val object could be changed, but the object itself is read-only.
Val cannot be reassigned
Book(name=Java, price=1000) Book(name=Kotlin, price=1000)