What is the difference between var and val in Kotlin?
What is the difference between var and val in Kotlin?
The main difference between var and val in Kotlin is that var declares a mutable variable whose value can be changed, while val declares an immutable variable whose value cannot be changed after it is assigned.
Some key points:
var variables can be reassigned to a new value at any point in the code. For example:var x = 5
x = 10 // OK
val variables are read-only and cannot be reassigned. Attempting to reassign a val will result in a compile error. For example:Đăng nhập để tham gia thảo luận cùng mọi người.
Chưa có bình luận nào
Hãy là người đầu tiên chia sẻ suy nghĩ của bạn.