Variables

Variables

  • It is not mandatory to define datatype while declaring variable
  • By default Any datatype is set for variable (Any is one datatype) in kotlin.
  • It will set Datatype of its first assigned value.
  • once datatype it set you can’t change datatype again.
  • You can also set datatype explicit
    • To set data dtype of variable use “ : “ operator followed by datatype (first letter of datatype must be capital)

Variable

  • “var” keyword is used to Declare variable in kotlin
  • Ex: var a
  • Ex: var b = 20
  • Ex: var c : String = “Hello”
  • variable are mutable so you can be reassigne its value

Value

  • “val” Keyword is used to Declare constant variable in kotlin
  • Ex: val a = 19
  • Ex: val name : String = “Jadav”
  • It must be intialized while declared
  • Value are Immutable so you cant reassigne its value again
 
 

→ For more example view

  • Kotlin / day1 / variable.kt