As you know, a variable is a location in memory (storage area) to hold data.


Variables in Kotlin

  • There are two keywords for variable declaration, var and val.
  • Use var when the variable value is to be modified and val where the variable value will not change after first assigned.
  • This val is similar to readonly keyword in C# or final keyword in Java.
  • val variable must be initialized at declaration.
  • Unlike Java or C#, you declare the type of a variable after the name, e.g. var firstName : String
  • Number primitive types are as follows: Double, Float, Long, Int, Short, Byte. There is no automatic conversion between types. You have to explicitly convert them.
  • More primitive types: Char, String, Boolean.
  • All variable declarations in Kotlin must be initialized.
  • The keyword void common in Java or C# is called Unit in Kotlin.

How to declare a variable in Kotlin?

To declare a variable in Kotlin, either var or val keyword is used. Here is an example:

var myString = "Hello World!"
val myNumber = 95

The difference in using var and val is discussed later in the article. For now, let's focus on variable declaration.

Here, myString is a variable of type String, and myNumber is a variable of type Int. You don't have to specify the type of variables; Kotlin implicitly does that for you. The compiler knows this by initializer expression ("Hello World!" is a String, and 95 is an integer value in the above program). This is called type inference in programming.

However, you can explicitly specify the type if you want to:

var myString: String = "Hello World!"
val myNumber: Int = 95

We have initialized variable during declaration in above examples. However, it's not necessary. You can declare variable and specify its type in one statement, and initialize the variable in another statement later in the program.

var myString: String      // variable declaration of type String
... .. ...
myString = "French"       // variable initialization

val myNumber: Int          // variable declaration of type Int 
... .. ...
myNumber = 95             // variable initialization

Here are few examples that results into error.

var language           // Error 
mySytring = "Hello World!"

Here, the type of myString variable is not explicitly specified, nor the variable is initialized during declaration.

var myString: String
myString = 14         // Error

​​​​Here, we are trying to assign 14 (integer value) to variable of different type (String).

Difference Between var and val

  • val (Immutable reference) - The variable declared using val keyword cannot be changed once the value is assigned. It is similar to final variable in Java.
  • var (Mutable reference) - The variable declared using var keyword can be changed later in the program. It corresponds to regular Java variable.

Here are few examples:

var myString = "Hello World!"
myString = "Hello Yugesh"

Here, myString variable is reassigned to Hello Yugesh. Since, the variable is declared using var, this code work perfectly.

val myString = "Hello World!"
myString = "Hello Yugesh"      // Error

You cannot reassign language variable to Hello Yugesh in the above example because the variable is declared using val.

Now, you know what Kotlin variables are, it's time to learn different values a Kotlin variable can take.