variables

23 0 0
                                        

1. val - It is a keyword used to indicate that the variable is a read only variable.

2. var - It is a keyword used to indicate that the variable's value can change.

syntax:

     a. declaration

          val name: Type

     b. initialization

          name = value

=============

fun main(){

     introducingVariables()

}

fun introducingVariables(){

     val name: String

     name = "Bell Cranel"

     var age = 60

     println(name)

     age = 40

     println(age)

}

==============

Result:

Bell Cranel

40

Note: When I initialize the variable age, I did not include the type since the compiler can inferred the type when I assigned a value to it. So, for constant values, the val keyword should be used.

Kotlin ProgrammingWhere stories live. Discover now