In this article, we will learn what are variables, how to declare them, what would be the scope of a variable.
Variable
Variables are used to store the data that would be referenced or manipulated by other statements within the scope of a variable. Let’s see an example of a variable in Kotlin:
fun main(args: Array<String>) {
val number: Int = 10
println(number * number) // prints 100
}
In line 2, you see a variable number is declared that holds a value 10. The variable is then referenced in Line 3, to print the square of the given variable.
Let’s dive into the declaration statement of the variable and understand each of the components involved.
Variable Declaration
Normally variable declaration comprises of keyword, variable name, variable type, assignment operator followed by its value as shown in the figure below. The order of the components should be preserved.
Keyword
Let’s start by discussing different keywords that we can use to declare variables. There are two keywords: val and var that can be used and they are discussed below:
- Using val keyword
When we declare a variable using the val keyword, it cannot be reassigned once it is initialized. These kinds of variables are called immutable variables.
Any attempt to reassign a new value to the variable declared as val will give a compile-time error. e.g.
- Using var Keyword.
However, if we declare a variable using var keywords then the variable is mutable, meaning it can be changed after it is initialized. E.g.
fun main(args: Array<String>) {
var number: Int = 10
number = 20
println(number * number) // prints 400
}
Variable name
A variable name can be any letters or digits or a combination of those. It can also have underscore along with letters or numbers.
Variable type
The variable type is optional to mention as Kotlin compiler can infer it based on the context. This capability of a compiler is known as Type Inference.
fun main(args: Array<String>) {
var number = 10
number = 20
println(number * number) // prints 400
}
As you see, the type of the variable number is not written because it is inferred.
Value
The process of assigning a value to a variable is called the initialization of a variable. The variable type and value should comply with each other. If the type of the variable is explicitly specified then the value should belong to the given type. If the type is not specified then the type will be inferred based on the used value.
Scope of a variable
Now that we have learned the various components of variable declaration. Let’s focus on the life of such variables. Variables are further distinguished into two categories based on their scope/ life. Let’s briefly discuss them as well.
Local Variable
When a variable is declared inside of a block then the variable is called a local variable. The scope of the local variable is within the same block. As you can see the fig 6, we can use variable number anywhere from lines 2 – 4. Any attempt to use it outside the block will show a compile-time error.
fun main(args: Array<String>) {
val number: Int = 10
println(number * number) // prints 100
}
fun demo() {
println(number
) // shows error because number is not defined
}
If you want to be able to use a variable inside different functions then declare the variable as a top-level variable.
Top-level Variable
When the variable is declared outside of any block, then it is called top-level variables. These variables can be referenced by any methods inside the same file.
val number: Int = 10
fun main(args: Array<String>) {
println(number * number) // prints 100
demo()
}
fun demo() {
println(number
) // prints 10
}
You can also declare the variable that is constant and known at compile time using the const keyword. You can only declare top-level variables as const as you can see in the example below.
const val number: Int = 10
fun main(args: Array<String>) {
println(number * number) // prints 100
demo()
}
fun demo() {
println(number
) // prints 10
}
Click on the next button to continue reading.
No responses yet