How to Declare Variables in Kotlin

By Mohammed Rashid •  August 24th, 2022 • 

Kotlin is the most popular programming language to develop Android apps. In this basic Kotlin tutorial, let’s check the usage of variables in Kotlin.

Variables in Kotlin are defined using either val or var keyword. The value of a variable declared using the val keyword cannot be changed later whereas the value of a variable with var can be changed anytime.

While declaring a variable you also have to mention the data type. See the variable declaration given below.

val count: Int = 2

You can declare the same using the var keyword too.

var count: Int = 2

You can also declare a variable without mentioning the data type. Kotlin identifies the data type automatically by evaluating the assigned value.

var count = 2

Then you can change the value of the variable (declared with var) as given below.

count = 3

That’s how you use variables in Kotlin.

Mohammed Rashid

Keep Reading