0 votes
in Kotlin by
What is the difference between var and val in Kotlin?

1 Answer

0 votes
by

var is like general variable and it's known as a mutable variable in kotlin and can be assigned multiple times.

val is like Final variable and it's known as immutable in Kotlin and can be initialized only single time.

+----------------+-----------------------------+---------------------------+

|                |             val             |            var            |

+----------------+-----------------------------+---------------------------+

| Reference type | Immutable(once initialized  | Mutable(can able to change|

|                | can't be reassigned)        | value)                    |

+----------------+-----------------------------+---------------------------+

| Example        | val n = 20                  | var n = 20                |

+----------------+-----------------------------+---------------------------+

| In Java        | final int n = 20;           | int n = 20;               |

+----------------+-----------------------------+---------------------------+

Related questions

0 votes
asked May 26, 2022 in Kotlin by Robin
0 votes
asked May 26, 2022 in Kotlin by Robin
0 votes
0 votes
asked May 26, 2022 in Kotlin by Robin
...