TExcelTAD
Home › Kotlin › Kotlin Variables

Kotlin Variables

A variable is a named box that holds a value. Kotlin has two keywords for creating one, and choosing between them is one of the first habits you'll build.

Keyword Meaning Can be reassigned?
val value — read-only no
var variable — mutable yes
kotlin · try it yourself
fun main() {
    val city = "Lisbon"     // read-only
    var temperature = 18    // mutable

    println("$city: $temperature °C")
    temperature = 22
    println("$city: $temperature °C")
}
Lisbon: 18 °C
Lisbon: 22 °C

val cannot be reassigned

Try to change a val and the compiler refuses — the program never runs:

kotlin · try it yourself
fun main() {
    val city = "Lisbon"
    city = "Porto"
    println(city)
}
File.kt:3:5: error: 'val' cannot be reassigned.

Tip: Prefer val everywhere and switch to var only when you genuinely need to change the value. Read-only variables make code easier to reason about — when you see a val, you know its value for the rest of the scope.

Type inference and explicit types

You rarely need to write a type: Kotlin infers it from the initial value. You can still spell it out after a colon, which is useful for documentation or when the inferred type isn't the one you want.

kotlin · try it yourself
fun main() {
    val name = "Ana"              // inferred: String
    val age: Int = 29             // explicit
    val height: Double = 1.68
    val likesTea: Boolean = true

    println("$name, $age, $height m, likes tea: $likesTea")
}
Ana, 29, 1.68 m, likes tea: true

Once a variable has a type, it keeps it. Assigning a value of another type is a compile error:

kotlin · try it yourself
fun main() {
    var count = 10
    count = "ten"
}
File.kt:3:11: error: Assignment type mismatch: actual type is 'String', but 'Int' was expected.

Declaring now, assigning later

A variable may be declared without a value as long as you give its type and assign it before reading it. The compiler tracks this for you:

kotlin · try it yourself
fun main() {
    val greeting: String
    val hour = 9
    if (hour < 12) {
        greeting = "Good morning"
    } else {
        greeting = "Good afternoon"
    }
    println(greeting)
}
Good morning

Even though greeting is a val, this is fine: it is assigned exactly once on every path.

Constants

For values known at compile time (numbers, strings, booleans) declared at the top level or in an object, use const val. By convention their names are in upper case:

kotlin
const val MAX_USERS = 100
const val APP_NAME = "Notes"

Naming rules

  • Names may contain letters, digits and underscores, but can't start with a digit.
  • Names are case-sensitive: total and Total are different variables.
  • Keywords such as val, fun or if can't be used as names.
  • Use descriptive camelCase names: orderTotal, not ot or x1.

Note: val means the variable can't be reassigned, not that the object it points to is frozen. A val list = mutableListOf(1) can still have items added — you just can't point list at a different list.

Test yourself

Which keyword declares a variable that can be reassigned?

  1. val
  2. var
  3. const
  4. let

What is the type of x in val x = 3.0?

  1. Int
  2. Float
  3. Double
  4. Number

What happens when you assign a String to a variable inferred as Int?

  1. It is converted automatically
  2. A compile error
  3. The program crashes at runtime
  4. The variable becomes Any