What is the difference between var and val in Kotlin?

When you create variables in Kotlin you can declare them with either var or val. The difference between these two keywords is that variables declared as var are mutable, while variables declared with val are immutable. This essentially means that the value of val variables cannot be changed, while var variables can be changed.

If you are experienced in Java you have probably come across the keyword final. You can think of val as a final variable. For example, consider an integer x that should be a constant versus an integer that we might want to change. The table below shows us how we would achieve this in Java and Kotlin.

final int x = 2;val x = 2
int x = 2;var x = 2


authors profile photo