Kotlin Variables Tutorial

In this section, we will learn what Variables are and how to use them in Kotlin.

What is Variable in Kotlin? (Defining Variable)

Any program you see out there works with data! For example, a calculator works with numbers. Or a movie player works with audio and video data and many more type of programs that each needs data in order to function correctly!

Now these programs, when they get data, they might not need it immediately! This means they need to store it in a place temporarily in order to access it in later time!

The place where data are stored is called memory!

Also, a program works with many numbers of data at each stage of its work! So there must be a way of tracking where each data is stored and how to retrieve it back from the memory!

This is where variables come in.

In short, a variable is a label on top of a portion of a memory! For now, think of it as a representative of a memory space! So if you want to store a data into a memory space, you create a variable and store the data into that variable!

Then in later time you can just call the variable to access the value currently stored there or update that value to something new.

In short, variables are our way of accessing the memory, storing values there and retrieve it in later time.

Kotlin Variable Syntax

This is how we can create a variable in Kotlin:

val variableName = value

var variableName: data-type = value

var variableName: data-type

Now let’s dissect this syntax and see what each part does.

Kotlin var and val keywords

First of all, in order to create a variable in Kotlin, we start with the keyword val or var.

If the keyword var is used to create a variable, that means we can reassign values any time we want to the target variable.

For example, if you created a variable using the var keyword and stored the value 10 in it, then in later time you can make the variable to hold another value like 400, etc.

But if the keyword val is used, that means you can only assign a value to the target variable once! After that, there’s no way to make the variable be reassigned to another value!

For example, if you stored the value 10 to a variable that is created using the val keyword, you cannot store another value like 400 in it! If you do so, you’ll get an error.

Note: we mainly use the val keyword when we want to create a variable with a value that we know will never change throughout the life cycle of the program! For example, if you have a variable named Pi that is storing the value 3.14, you’re rest assured that the value of this variable won’t change at all, then you can create the variable using the valkeyword.

Note: we use the var and val keywords only when declaring a variable! After the declaration, you don’t need to use these keywords when accessing the target variable (for either assigning a value to it or retrieving the current value that is stored there).

Identifiers in Kotlin

The name of a variable is known as its identifier! For example, if the name of a variable is firstName then we can say that the variable’s identifier is firstName.

When creating a new variable, its name (identifier) comes after the keyword var or val.

For example:

var firstName

val lastName

Here we’ve created two identifiers named firstName and lastName, respectively.

Data Types of Variables in Kotlin

A data type, as the name suggests, is the type of value we want to store into a variable! For example, a number like 10 is of type Int (which stands for integer), or a value like “this is a sentence” is of type String.

So, whenever creating a new variable in Kotlin, its data type needs to be declared as well.

This process is done either implicitly (Kotlin can infer the data type of variable) or explicitly (we can define the data type of variable exactly what it should be).

The data type of variable needs to be clear because:

  • Each data type takes a different amount of memory space, and Kotlin needs to know how much memory space it should set aside for a variable! So we set the data type of a variable, and Kotlin uses this knowledge to allocate the right amount of memory for that variable.
  • Setting the data type of variable also helps compilers to control a source code and see if variables are holding the right value and alert developers if a variable taking a value with different data type!
  • Also, each data type has a different set of behavior! For example, you can invoke an integer data type into a mathematical operation, but you can’t use a string data type into this type of operation! On the other hand, we can change the letters of a string data type into uppercase or lowercase, but the same is not possible for a value of type integer, for example. So yes, depending on the type of a value, we will have different set of behaviors available to be used for different purposes.

Now, if the purpose is to let Kotlin itself infer the data type then we need to assign a value to a variable right when that variable is being declared!

For example:

var firstName = “John”

Here the name of the variable is firstName; we used the keyword var` to create the variable, so it means the variable is capable of reassigning new values. And we’ve assigned a value right where the variable is declared.

Now using this value, Kotlin knows that the data type of this variable will be String

The second way of setting the data type of a variable is by explicitly defining it:

var age: Int

As you can see, after the name of the variable we put a colon : and after that comes the name of the target data type (we used the Int data type in this example).

For this example, we explicitly set the data type of the age variable to Int which stands for integer and that means we’re only allowed to enter whole numbers (integer numbers) as the value of this variable.

Note: after the data type of variable is cleared out, you cannot store a value of different data type in it! If you do so, you’ll get an error!

Note: when explicitly setting the data type of variable, storing a value to that variable becomes optional and can be done in later time.

For example:

var firstName:String = “John”

But remember: this is only true if the variable is created using var keyword and not val!

For variables that are created using val we need to set their values right at the declaration time no-matter if the data type is set or not.

Declaring (creating) Variables in Kotlin

The process of creating a variable is called declaration. Get use to this word because most of the times this is what we and developers use instead of the word “create”.

Example: creating Kotlin Variables

fun main(){
    var firstName = "John"
    val lastName:String = "Doe" 





In this example, we have two variables named firstName and lastName. Both are of type String (one implicitly set and the other explicitly).

Also, the value of the firstName variable is set to “John” and the value of the lastName is set to “Doe”

Note: As mentioned before, variables are a pointer to a memory location and we said you can think of them as a label in a memory space.

So in this example, what happened is that the value “John” is stored in a memory location and the firstName variable is pointer to that memory location and hence is the representation of this value!

This means if we need this value, all we need to do is to simply call the variable in the place where the value is needed! Your program is then smart enough to use the variable name to find the memory location and take out the actual value that is stored there!

The same process is true for the lastName variable.

Variable Initialization

Assigning a value to a variable for the first time is called variable initialization.

Be aware that if the variable is created using the val keyword, then we need to assign a value to that variable right when the declaration is happening!

But if the variable is created using the var keyword, we’re free to initialize the variable at later time.

Note: again, if you’re creating a variable using the var keyword and want to initialize it at later time, then make sure you set the data type of that variable when it is being declared.

Example: initialization of Variable in Kotlin

fun main(){
    var firstName:String
    var age: Int 
    val company = "Apple"

    firstName = "John"
    age = 100

    println("The name is: ${firstName} and the age is: ${age} and the company is: ${company}")


The name is: John and the age is: 100 and the company is: Apple

Here, the firstName and age variables are declared first and then, in another statement, they are initialized.

But the company variable because it’s a constant (meaning it can take a value just once) we’ve initialized it right at the time of declaration.

Kotlin Variables Naming Conventions

When setting names for variables in Kotlin, there are a couple of rules that you need for follow:

  • The name of a variable can contain letters, numbers and underscores _.
  • It can start with character or underscore, but not numbers!
  • Although you can create a variable with a capital letter, the convention is to use the lowercase letter as the first character of a variable.
  • We can’t put space between the characters of a variable! If we do so, we will get error.
  • The name of a variable can’t contain symbols like: !@#$%^&*()?/<>\
  • If a variable’s name contains multiple words, the convention is to use the camelCase format, which means the first character of the first word starts with a lowercase letter and for the rest of words the first character must be an uppercase letter. In short, this convention helps the readability of the variables.
  • Also, we can’t use the reserved words that Kotlin itself is using for other purposes. These keywords are mentioned in the section below.

Kotlin Reserved Words

In Kotlin, there are a couple of reserved words that you can’t use them as the name for variables. This is because Kotlin itself already using it for other purposes.

These words are:

































Kotlin Variable Assignment

The process of adding a value to a variable is called variable assignment!

This is done using the = operator, which in programming it is called the assignment operator.

For example:

var salary = 100000

Here the variable name is salary and we’ve assigned the value 100000 to it using the assignment operator.

Note that if there’s a value currently in a variable and we reassign a new value to that variable, the old value will be gone and the variable will point to the new one.

For example:

 fun main(){
    var age: Int

    age = 10




Note that the age variable at first had the value 100. But then we’ve reassigned the value to 40. Now, whenever we call the variable age, we get the value 40 as a result.

Kotlin Variable Reading

Now the process of getting the current value of a variable is called variable reading!

Be aware that other than on the left side of the assignment operator, in any place we invoke a variable, we’re reading the current value.

Note: reading a value in a variable won’t remove that value from the variable, though! We just get a reference to that value!

For example:

fun main(){
    var age: Int

    age = 10




Here, the value of the age variable is 10 and when we passed the variable to the println function, we got the value 10 showed on the output stream.

Note that we can pass the current value of a variable as the argument to another variable as well.

For example:

fun main(){
    var age: Int

    age = 100
    var similarAge = age




Here, the value of the similarAge variable is taken from the current value of the age variable, which is 100.

For this reason, when we called the similarAge variable to get its value, we got the value 100.

Kotlin var vs val

In short, we use the var keyword whenever we want to create a variable that is capable of taking and changing values at any time.

But we use the val keyword when we want to create a variable that its value will never change! These types of variables are called “constant identifiers”.


Top Technologies