In Kotlin either var or val keyword is used to declare the variable.
We don’t want to specify the type of variable, Kotlin implicitly conveting to the proper data types.
val in Kotlin : The variable declared using val keyword cannot be changed once the value is assigned.
It is similar to final variable in Java.
var in Kotlin – The variable declared using var keyword can be changed later in the program. It corresponds to regular Java variable.
Go through the above program to get more understanding about Kotlin var and val.
Kotlin Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | fun main(args : Array<String>) { var name = "Kotlin" val cash = 95 var percentage = 10.34 println(name+"-"+cash+"-"+percentage) var language: String // String Variable Declaration language = "english" // Initializing the variaable val score: Int // Int variable Declaration score = 200 // Initializing the variaable } |
Output:
1 2 3 | Kotlin-95-10.34 |