Beginner 8 min readKotlin 2.0

Kotlin Variables — val, var, and Type Inference Explained

Kotlin has two ways to declare variables: val for read-only values and var for mutable ones. This tutorial explains both with practical examples.

What You Will Learn

  • How to declare variables with val and var
  • The difference between val and var
  • How type inference works in Kotlin
  • When to use explicit types
  • Common variable mistakes to avoid

Declaring Variables in Kotlin

In Kotlin, you declare variables using two keywords: - **val** — read-only (like a constant after assignment) - **var** — mutable (can be reassigned) Kotlin can infer the type automatically from the assigned value, so you rarely need to write the type explicitly.

Syntax

kotlin
val variableName = value
var variableName = value
val variableName: Type = value

Basic Variable Declaration

kotlin
fun main() {
    val language = "Kotlin"
    var year = 2026

    println(language)
    println(year)

    year = 2027
    println(year)
}
Output
Kotlin 2026 2027

language is a val — it cannot be reassigned after declaration. year is a var — it can be updated, and we change it from 2026 to 2027.

Beginner Tip: Default to val. Switch to var only when you genuinely need to change the value later. This habit makes your code safer and easier to understand.
Common Mistake: Trying to reassign a val causes a compile error: "Val cannot be reassigned". Always check which keyword you need before declaring.

Type Inference

Kotlin automatically infers the type from the assigned value. When you write val name = "Kotlin", Kotlin knows name is a String. You do not need to write val name: String = "Kotlin" unless you want to be explicit.

Inferred vs Explicit Types

kotlin
fun main() {
    val name = "Kotlin"          // inferred as String
    val age: Int = 25            // explicit Int
    val price = 9.99             // inferred as Double
    val isActive = true          // inferred as Boolean

    println(name)
    println(age)
    println(price)
    println(isActive)
}
Output
Kotlin 25 9.99 true

Kotlin infers the type from the right-hand side value. You can still write the type explicitly if it makes the code clearer — both styles are valid.

Beginner Tip: Use explicit types when the type is not obvious from the value, or when you want to declare a variable without assigning a value immediately.

Declaring Without Immediate Assignment

Sometimes you want to declare a variable and assign it later. In this case, you must write the type explicitly — Kotlin cannot infer the type from nothing.

Declare First, Assign Later

kotlin
fun main() {
    val result: Int
    result = 10 + 5
    println(result)
}
Output
15

result is declared as Int but assigned later. Kotlin ensures you cannot read result before it is assigned — this prevents uninitialized variable bugs.

For var, you must assign before reading. For val, you can declare first and assign once — but not twice.

Practice Exercise

Exercisefix bug

Fix the bug in this code: val score = 100 score = 200 println(score)

kotlin
val score = 100
score = 200
println(score)

Quick Quiz

Quick Quiz

Which keyword declares a read-only variable in Kotlin?

Frequently Asked Questions

Related Tutorials

Last updated: 2026-05-01Kotlin 2.0

Written by KotlinGuide Editorial Team · Reviewed by KotlinGuide Technical Review