Updated March 2, 2023
Difference Between Kotlin val vs var
Kotlin val vs var is the general variable, and it was assigned multiple times also it is known as a mutable variable in a kotlin. Kotlin val is the constant variable which was assigned multiple times and which was initialized in a single time and it is also known as variable of immutable in kotlin. Kotlin val is assigned one; this was read-only and contains the mutable in nature.
What is Kotlin val?
The object which we are storing by using kotlin val is not changed; we cannot reassign the same it is the same as the final keyword which we are using in java. Kotlin val is immutable, so once we have assigned the val, it will become read-only. The property of val object is changed, but the object is read-only. The below example shows kotlin val as follows. In below example we can see that first we have defined the per value as 75 and then we have defined the per val as 85, it will give the error val cannot be reassigned. Basically we are using val in kotlin for declaring a variable, val is the consistent variable and which we are assigning multiple times.
Code:
fun main()
{
val per = 75
println ("Marks" + per)
per = 85
println ("new per " + per)
}
Output:
What is var?
In kotlin language var is used to declare the variables. Var is a general variable defined in the language of kotlin. The variable value is declared using var, and we can change the same throughout the program. In kotlin language var is also called as non-final variable and mutable, as we can change the var value anytime in a program. Below example shows kotlin var as follows. In the below example we can see that it will display the output of previous as well as new values. So we can say that we can change the value at any time in the program by using kotlin var. In below example first we are setting the per value as 65 then we are setting the value as 75 then in our output both value will be displayed.
Code:
fun main()
{
var per = 65
println ("Per is " + per)
per = 75
println ("New per " + per)
}
Output:
Head-to-Head Comparison Between Kotlin val vs var (Infographics)
Below are the top 12 differences between Kotlin val vs var:
Key Difference Between Kotlin val vs var
Let’s see the key differences between Kotlin val vs var:
- In kotlin, we can declare the variable by using two different keywords first is val, and the second is var. In kotlin var is variable which we are assigning multiple times in our program, whereas val is the constant variables in kotlin language, which we cannot assign multiple time in our program. In kotlin, we are using val for defining the variables which were read-only; in those variables, we can assign value only once. Kotlin var is not a read-only variable; we are assigning the value to the var variable multiple times in our code. In the kotlin language, var and val are important variables for assigning the values.
- In kotlin, var variable scope at the global level into the file. For example at the time of declaring variable at top level of the code we can access the same into the main as well as test methods. In kotlin val variable also contains the scope at the global level in the file. For example, at the time of declaring the val variable at the top level of the code, we can access the same in the main or test methods, but we cannot reassign the same throughout the code.
- Suppose we want to declare the class properties as mutable, then we need to use the var keyword or if suppose we want to declare the class properties as read-only then we need to use the val keyword. As we are defining the final keyword in java for read only purpose same we are using val keyword in kotlin.
- In kotlin, val and var declaration do not differ from the visibility type, but they will differ in mutability. Kotlin is offering the built-in functionality which contains immutability. We can achieve those properties by declaring the val and var variables. In kotlin val and var both the variable will declare the keyword which was allowing us to declare the visibility modes which was differing from the mutable properties.
Kotlin val vs var Comparison Table
Let’s discuss the top comparison between Kotlin val vs var:
Sr. No | Kotlin val | var |
1 | In the kotlin language, val variable is immutable. | In the kotlin language, val variable is mutable. |
2 | The syntax of declaring val variable is:
val var_name = “var_value” |
The syntax of declaring var variable is:
var var_name = “var_value” |
3 | By using kotlin val variable, we are not assigning the variable multiple times. | By using kotlin var variable we can assign variable multiple times. |
4 | We cannot reassign the val variable in our program. | We are reassigning var variable in our program. |
5 | In kotlin, we are declaring the val variable by using the val keyword. | In kotlin, we are declaring the var variable by using var keyword. |
6 | In kotlin, we are assigning the value of the val variable at runtime. | In kotlin, we are assigning the value of the var variable at runtime. |
7 | We can declare variable with val also we are assigning the primitive data types to it. | We can declare variables with var also, we are assigning the primitive data types to it. |
8 | We can declare variable with val also we are assigning function to it. | We can declare variables with var also, we are assigning function to it. |
9 | Example of declaring val variable is:
val no = 45 |
Example of declaring var variable is:
var no = 35 |
10 | The val keyword’s actual name is value in kotlin. | The var keyword actual name is variable in kotlin. |
11 | By using val, we are storing the object into a variable. | By using var, we are storing the object into value. |
12 | We are using val in integer as well as string values. | We are using var in integer as well as string values. |
Conclusion
In kotlin language var is used to declare the variables. Var is a general variable defined in the language of kotlin. The object which we are storing by using kotlin val is not changed; we cannot reassign the same it is the same as the final keyword which we are using in java.
Recommended Articles
This is a guide to Kotlin val vs var. Here we discuss Kotlin val vs var key differences with infographics and a comparison table, respectively. You may also have a look at the following articles to learn more –