Kotlin Variables Example

Chapter: Kotlin Last Updated: 11-11-2017 18:12:26 UTC

Program:

            /* ............... START ............... */
                
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

}

                /* ............... END ............... */
        

Output

Kotlin-95-10.34

Notes:

  • 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.

Tags

Variables Example , Kotlin, var, val

Similar Programs Chapter Last Updated
Kotlin Continue Kotlin 02-03-2018
Kotlin Break Statement Kotlin 27-01-2018
Kotlin For Loop Kotlin 30-11-2017
Kotlin Do While Loop Kotlin 19-11-2017
Kotlin While Loop Kotlin 19-11-2017
Kotlin When Statement Kotlin 18-11-2017
Kotlin Nested If Kotlin 18-11-2017
Kotlin If Else If Statement Kotlin 18-11-2017
Kotlin If Statement Kotlin 17-11-2017
Kotlin Print And Println Kotlin 17-11-2017
Kotlin Type Conversion Kotlin 17-11-2017
Kotlin Logical Operators Kotlin 17-11-2017
Kotlin Assignment Operators Kotlin 15-11-2017
Kotlin Arithmetic Operators Kotlin 15-11-2017
Kotlin Data Types Kotlin 12-11-2017
Kotlin Comments Example Kotlin 11-11-2017
Kotlin Hello World Kotlin 11-11-2017

1