Variables

In programming domain, a variable is just reference to some address in the memory. That memory can thus hold some data.
Kotlin's way of working with variables is a little bit unique. First in Kotlin, you start a variable declaration with keyword, not a type like in java. Secondly, in kotlin, you can optionally specify the type after the variable name.
Let's see a quick example:

val topic="Enlightment and Self Inquiry"
val age=50

In the above example, take note that we don't specify the type. Kotlin will implicitly infer the data types based on the values we have assigned our variables,

val topic: String="Enlightment and Self Inquiry"
val age: Int=50

In the above example, we explicitly specify the type. This optional.

However, if you chose to just declare a variable and defer the initialization to later in your code, then it is a MUST to explicitly specify the type. This is important because otherwise the compiler has no way infering the type you are using for your variable. See the below example:

val topic: String
val age: Int

topic="Enlightment and Self Inquiry"
age=50

Variable Mutability and Immutability

We can classify variables in Kotlin in two categories: Mutable and Immutable Variables.
Mutable variables can be changed/mutated while immutable variables cannot. Beware that a variable is a reference to a memory address, so we are talking about the mutability of the references and not the objects held in those references.
We use two keywords to create variables based on this:

  1. val - Used to create immutable variables/references. Variables declared using this keyowrd cannot be reassigned once it's been initialized with a value. This is the equivalence of final variables in java.
  2. var - Used to create mutable variables. These are regular variables and their values can be reassigned after initialization.
//Immutable variable
val topic1 = "Enlightment and Self Inquiry"

//mutable variable
var topic2 = "Enlightment and Self Inquiry"

//This gives Error
topic1 = "New Topic one"

//This is alright
topic2 = "New Topic two"

//Error because of type mismatch
topic2 = 5