Saturday 18 November 2017

Define variables in Kotlin

Variable is a named memory location used to store data.

Syntax
var variableName : DataType = value
val variableName : DataType = value

Both ‘var’ and ‘val’ are the keywords.

Ex
var a : Int  =10;
‘a’ is of type int and assigned with value 10.

val b : Double = 1.23
‘b’ is of type Double and assigned with value 1.23. The keyword ‘val’ is used to define constants (immutable objects).


HelloWorld.kt

fun main(args: Array<String>) {
 var a : Int  =10;
 val b : Double = 1.23
 
 println("a : ${a}")
 println("b : ${b}")
 
}


C:\>kotlinc HelloWorld.kt

C:\>kotlin HelloWorldKt
a : 10
b : 1.23


Previous                                                 Next                                                 Home

No comments:

Post a Comment