Variables are an important part of any programming. They are the names you give to computer memory locations which are used to store values in a computer program and later you use those names to retrieve the stored values and use them in your program.
Kotlin variables are created using either var or val keywords and then an equal sign = is used to assign a value to those created variables.
Syntax
Following is a simple syntax to create two variables and then assign them different values:
var name ="Zara Ali"var age =19var height =5.2
Examples
Once a variable is created and assigned a value, later we can access its value using its name as follows:
funmain(){var name ="Zara Ali"var age =19println(name)println(age)}
When you run the above Kotlin program, it will generate the following output:
Zara Ali
19
Let’s see one more example where we will access variable values using dollar sign $:
funmain(){var name ="Zara Ali"var age =19println("Name = $name")println("Age = $age")}
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Let’s see one more example to display the variable values without using a dollar sign as below:
funmain(){var name ="Zara Ali"var age =19println("Name = "+ name)println("Age = "+ age)}
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Kotlin Mutable Variables
Mutable means that the variable can be reassigned to a different value after initial assignment. To declare a mutable variable, we use the var keyword as we have used in the above examples:
funmain(){var name ="Zara Ali"var age =19println("Name = $name")println("Age = $age")
name ="Nuha Ali"
age =11println("Name = $name")println("Age = $age")}
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Name = Nuha Ali
Age = 11
Kotlin Read-only Variables
A read-only variable can be declared using val (instead of var) and once a value is assigned, it can not be re-assigned.
funmain(){val name ="Zara Ali"val age =19println("Name = $name")println("Age = $age")
name ="Nuha Ali"// Not allowed, throws an exception
age =11println("Name = $name")println("Age = $age")}
When you run the above Kotlin program, it will generate the following output:
main.kt:8:4: error: val cannot be reassigned
name = "Nuha Ali" // Not allowed, throws an exception
^
main.kt:9:4: error: val cannot be reassigned
age = 11
^
Read-only vs Mutable
The Mutable variables will be used to define variables, which will keep charging their values based on different conditions during program execution.
You will use Read-only variable to define different constant values i.e. the variables which will retain their value throughout of the program.
Kotlin Variable Types
Kotlin is smart enough to recognise that “Zara Ali” is a string, and that 19 is a number variable. However, you can explicitly specify a variable type while creating it:
funmain(){var name: String ="Zara Ali"var age: Int =19println("Name = $name")println("Age = $age")
name ="Nuha Ali"
age =11println("Name = $name")println("Age = $age")}
When you run the above Kotlin program, it will generate the following output:
Name = Zara Ali
Age = 19
Name = Nuha Ali
Age = 11
Soon we will learn more about different data types available in Kotlin which can be used to create different type of variables.
Kotlin Variable Naming Rules
There are certain rules to be followed while naming the Kotlin variables:
- Kotlin variable names can contain letters, digits, underscores, and dollar signs.
- Kotlin variable names should start with a letter or underscores
- Kotlin variables are case sensitive which means Zara and ZARA are two different variables.
- Kotlin variable can not have any white space or other control characters.
- Kotlin variable can not have names like var, val, String, Int because they are reserved keywords in Kotlin.
Leave a Reply