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.

Example:

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.