Kotlin Variables: Everything You Need to?Know
What is a variable?
A variable is container for data. In simpler words a variable holds certain value.?
Declaration and Assignment of?variable
val a : Int //declared a variable of type Int
a = 10 //assigned value 10 to variable a
val b = 10 //declaration and assignment of variable in single step
Above code is does two things?:
Val keyword in?Kotlin
Variables declared as val are read only variables, which cannot be changed after it has been initialised.
Variables declared as val cannot be reassigned the value but they can be accessed as many times as we need. The complier will throw an error in the case if we try to reassign value to val variable.
It is important to note that val is not a synonym of immutable.?
In the following example, we will use a MutableList, which contains elements of the same type. The second line won’t compile, since we are trying to reassign a val variable.?
However, it is always possible to change the internal state of a val variable: while it is prohibited to reassign the variable, its content can be modified in other ways.
So, the following code is correct:
As you can see, this code changed the internal state of the primeNumbers by adding another prime number. When invoked the add() function, we changed not the variable itself but the list it represents.
Var keyword in?Kotlin
Variable declared as var can be reassigned a value, which can be changed as many times as needed.
It’s a good practice and also recommend by Kotlin to declare variable as val and not as var unless you have a use case to reassign the variable. As val makes sure the variable is not accidentally updated.
Type Inference in?Kotlin
Kotlin is a type inferred language i.e Kotlin compiler automatically infers the type of variable if we declare and assign the value of variable together.
领英推荐
val a = 10
In the above code, Kotlin compiler automatically deduces that variable a is of type Int since the default data type for whole numbers in Kotlin is Integer.
This feature reduces boilerplate code and enhances code readability.
Other way to let the compiler know the type of variable is explicitly specifying the type of variable.
val a : Int
In the above code, it is explicitly specified that type of variable a will be Int. Additionally, value of variable can be assigned later.
Now, we can do both declare the type and assign value at the same time.
val a : Int = 10
The syntax provided above is technically valid but not advisable unless you have a specific need to explicitly define the variable’s type. When assigning a value, Kotlin’s compiler can automatically deduce the variable’s type. Using the mentioned syntax involves unnecessary redundancy, which goes against Kotlin’s goal of minimising manual typing. Therefore, it is recommended to write it in the following way to avoid providing redundant information to the compiler:
val a = 10
In some special cases you might want to explicitly define the type of variable. For example by default the data type of whole number is Int but you want the variable to be of type Short in this case you would need to explicitly define the value of variable.
val a : Short = 10
Type of variable cannot be changed. The complier can infer type of variable but that doesn’t mean we can change the type of variable.
In the above code, complier automatically infers type of variable as Int. If an attempt is made to assign a String value to this variable, the compiler will generate an error because it’s not allowed to assign a String to a variable declared as Int.It’s important to note that even if the variable is declared as var, the type cannot be changed?
Summary
We looked at?:
Now, that we have known about variables. Stay tuned for next to learn about data types in Kotlin!