Kotlin var vs val – Difference between var and val

Here you will learn about difference between var and val in Kotlin i.e. var vs val.

Both var and val are keywords that are used to declare variable in Kotlin. But there is a little difference between them as mentioned below.

var: Used to declare a mutable variable. It means the value of variable can be changed multiple times.

val:¬†Used to declare a read only variable. It means once the value is assigned to variable, that can’t be changed later.

val is same as like final keyword in Java.


var v1 = 10
v1 = 20	//works fine

val v2 = 15
v2 = 30 //it will show error, "Val cannot be reassigned"

Leave a Comment

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