0Pricing
Kotlin Academy · Lesson

val vs var: Immutability From Day One

Understand the difference between val and var and why immutability matters.

val vs var: Immutability From Day One is a free Kotlin Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Two Ways to Declare a Variable

Kotlin has two keywords for declaring variables: val for read-only (immutable) references, and var for mutable ones. Choosing correctly matters.

val: Read-Only Reference

val declares a reference that cannot be reassigned after initialization. Think of it like final in Java.
val name = "Kotlin"
println(name)
// name = "Java" // Error: Val cannot be reassigned

var: Mutable Reference

var declares a variable that can be reassigned. Use var only when you genuinely need mutation.
var counter = 0
counter = 1
counter += 1
println(counter) // 2

Type Inference

Kotlin infers the type from the initial value. You can also write the type explicitly after a colon.
val age = 25          // inferred: Int
val name: String = "Ana"  // explicit type
var score: Double = 0.0

val Does Not Mean Immutable Object

val prevents reassignment of the reference, but the object itself can still be mutable. A val MutableList can still have items added.
val list = mutableListOf(1, 2, 3)
list.add(4)        // OK! list is val but mutable
// list = mutableListOf() // Error: reassignment

Prefer val by Default

Kotlin style guides recommend defaulting to val. It makes code safer and easier to reason about. Only upgrade to var when mutation is required.
val pi = 3.14159     // constant value
val greeting = "Hi"  // never changes
var retries = 3      // will be decremented

val vs const val

const val is for compile-time constants (primitives and String at top-level or in object/companion). val is a runtime constant.
const val MAX_SIZE = 100    // compile-time
val instance = MyClass()    // runtime

Uninitialized var

You can declare a var without initializing it, but you MUST provide a type and assign before use. val must always be initialized.
var result: String
result = "computed"
println(result)

Scope and Shadowing

Variables are scoped to their block. Inner scopes can shadow outer variables — but this can be confusing, so use meaningful names.
val x = 10
run {
    val x = 20  // shadows outer x
    println(x)  // 20
}
println(x)      // 10

Naming Conventions

Kotlin uses camelCase for variables and functions. Constants (const val) use UPPER_SNAKE_CASE. Classes use PascalCase.
val firstName = "Jane"     // camelCase
const val MAX_ITEMS = 50   // UPPER_SNAKE_CASE
class UserProfile           // PascalCase

Why Immutability Matters

Immutable data is thread-safe by default, easier to test, and reduces bugs from unexpected mutation. Always ask: do I need to change this value?

Quick Check

Which keyword declares a read-only (non-reassignable) reference in Kotlin?

Recap

val = read-only reference, var = mutable. Prefer val. Type inference lets you skip explicit types. Next: Kotlin's built-in types!

Frequently asked questions

Is the “val vs var: Immutability From Day One” lesson free?

Yes — the full text of “val vs var: Immutability From Day One” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “val vs var: Immutability From Day One”?

Understand the difference between val and var and why immutability matters. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “val vs var: Immutability From Day One” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Setting Up Kotlin: REPL, IntelliJ & Online Playground
  2. val vs var: Immutability From Day One
  3. Kotlin Basic Types: Int, Double, Boolean, Char
  4. Your First Kotlin Program: main, println & Input
← Back to Kotlin Academy