Kotlin: val vs var

What's the difference?

Kotlin provides two ways to define variables: val and var. What's the difference? You can change var variables as many times as you want. But once you set a val variable, you can't update again. Here's an example:

val firstName = "Daniel" // always stuck with "Daniel", can't change anymore

From now on, you can't change firstName; it's set in stone. However, had I created firstName with var, I can change it:

var firstName = "Daniel"
firstName = "Dan"
firstName = "John"
firstName = "Don"

In general, prefer val, unless the variables needs to change.


More Tutorials

Build a Ridesharing Android App - Part 1

Getting started is sometimes the hardest part.


Add a click listener in Kotlin.

Android Login Layout

Let users sign in.