kotlin difference between val and var

Solutions on MaxInterview for kotlin difference between val and var by the best coders in the world

showing results for - "kotlin difference between val and var"
Nele
06 May 2016
1// Kotlin difference between val and var
2
3// val stands for read-only variable, and cannot be changed. (immutable variable)
4fun main() {
5    val number = 1
6    number = 2	 // Not possible.
7}
8  
9// var stands for variable, and can be changed. (mutable variable)
10fun main() {
11    var number = 1
12    number = number + 9
13    println(number) // this would return 10, because number got changed
14}
Ella
07 May 2018
1// Kotlin difference between val and var
2
3// val stands for value, and cannot be changed. (immutable variable)
4fun main() {
5    var number = 1
6}
7  
8// var stands for variable, and can be changed. (mutable variable)
9fun main() {
10    var number = 1
11    number = number + 9
12    println(number) // this would return 10, because number got changed
13}
14