Cookie Preferences

We use cookies to enhance your experience. Choose your preference for cookie usage.

Essential cookies are required for basic functionality. Additional cookies help us improve our service and provide analytics.

View third-party services
  • Google Analytics: Website traffic analysis and conversion tracking
  • RudderStack: Analysis of user interactions with the website
  • Microsoft Clarity: User behavior analysis & session recordings
  • Facebook Pixel: Marketing analysis of user activity
KotlinData types and variables

Values and variables

What is a variable?

A variable is placeholder for storing a value like a string, a number, or something else. Every variable has a name (or identifier) to distinguish it from other variables. It is possible to access a value by its variable's name.

Variables are one of the most often used elements of programs; therefore it is important to understand and be able to use them.

Declaring variables

Before you start using a variable, you must declare it. To declare a variable, Kotlin provides two keywords:

  • val (from value) — declares an immutable variable (just a named value or constant) that cannot be changed after it has been initialized;
  • var (from variable) — declares a mutable variable that can be changed (as many times as needed);

When you declare a variable, you must add its name after one of these keywords. The name of a variable cannot start with a digit. Usually, it starts with a letter. Always try to choose meaningful and readable names for variables to make your code easy to understand.

To assign a certain value to a variable, we should use the assignment operator =.

Let's declare an immutable variable named language and initialize it with the string "Kotlin".

val language = "Kotlin"
Note that the case of a variable is important: language is not the same as Language.

Now we can access this string by the variable's name. Let's print it!
println(language) // prints "Kotlin" without quotes
This variable cannot be changed after it has been initialized because it has been declared with val.

Let's declare a mutable variable named dayOfWeek and print its value before and after changing it.

var dayOfWeek = "Monday"
println(dayOfWeek) // Monday

dayOfWeek = "Tuesday"
println(dayOfWeek) // Tuesday
In the example above, we declared a variable named dayOfWeek and initialized it with the value "Monday". Then we accessed the value by the variable name to print it. After that, we changed the variable's value to "Tuesday" and printed this new value.

Note that you do not need to declare a variable again to change its value. Just assign a new value to it using the = operator.

It is also possible to declare and initialize a variable with the value of another one:

val cost = 3
val costOfCoffee = cost
println(costOfCoffee) // 3
Variables can store not only strings, but also numbers, characters, and other data types, which we will learn about in this course.

Let's declare three immutable variables to store a number, a string, and a character, and then print their values.

val ten = 10
val greeting = "Hello"
val firstLetter = 'A'

println(ten) // 10
println(greeting) // Hello
println(firstLetter) // A
There is one restriction for mutable variables (which are declared with the keyword var). You can only reassign values of the same type as the initial value. So, the following code is not correct:

var number = 10
number = 11 // ok
number = "twelve" // an error here!
Please remember this restriction!

Battle of keywords: val vs var

As you saw above, there are two keywords which can be used to declare variables. Actually, in many cases, it is better to use immutable variables declared with the keyword val. Before using var you should be sure that val is not suitable in that case. If it isn't, then use var. But keep in mind, the more mutable variables in your program, the harder it will be to understand it. By the same token, immutable variables make your program easier and help to maintain a readable code. So, use val whenever possible! You can easily replace it with var when it becomes necessary.
2 learners liked this piece of theory. 0 didn't like it. What about you?
Report a typo