Variables (val & var)

Kotlin is a modern, concise language that gives you two ways to name data: val for read-only values and var for ones you can change.

Learn Variables (val & var) in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll know exactly when to reach for each, how type inference saves you keystrokes, and how Kotlin's basic types — Int, Double, Boolean, Char, and String — work.

What You'll Learn in This Lesson

1️⃣ val vs var

Kotlin has exactly two keywords for declaring variables. val creates a read-only reference that can be assigned once. var creates a mutable reference you can reassign as many times as you like. The rule of thumb: prefer val , and switch to var only when you must.

The last two lines are the subtle part: val items can't point at a different list, but the list itself is mutable, so add still works. val freezes the reference , not the contents.

2️⃣ Types and Type Inference

Kotlin is statically typed — every variable has a type — but you rarely have to write it. The compiler infers the type from the value. When you do want to be explicit, add a colon and the type after the name. Kotlin's basic types include Int , Long , Double , Boolean , Char , and String .

Notice Kotlin won't silently widen numbers: to treat an Int as a Double you call .toDouble() . This prevents accidental precision loss.

Your turn. Fill in the TODO s, following the 👉 hints, then run and compare.

Use what you've learned about val , var , and types to build a small total. Write it yourself and check it against the example output.

📋 Quick Reference — Variables

Practice quiz

What does val declare in Kotlin?

  • A read-only reference you cannot reassign
  • A mutable variable
  • A function
  • A constant only known at runtime

Answer: A read-only reference you cannot reassign. val creates a read-only reference assigned once.

Which keyword lets you reassign a variable later?

  • val
  • var
  • const
  • let

Answer: var. var creates a mutable reference you can reassign.

What happens if you try to reassign a val?

  • It silently works
  • It compiles with a warning
  • A compile error: Val cannot be reassigned
  • It becomes a var

Answer: A compile error: Val cannot be reassigned. Reassigning a val is a compile-time error.

What does Kotlin's type inference let you do?

  • Skip declaring variables
  • Convert types automatically
  • Run without a compiler
  • Omit the type when it is clear from the value

Answer: Omit the type when it is clear from the value. val x = 5 infers Int, so you rarely write the type explicitly.

Does val make the object it points to immutable?

  • No, it only stops reassigning the reference; a mutable object can still change
  • Yes, everything becomes frozen
  • Only for numbers
  • Only inside functions

Answer: No, it only stops reassigning the reference; a mutable object can still change. val freezes the reference, not the contents; a mutable list can still be modified.

Which literal correctly declares a Char?

  • "N"
  • 'N'
  • Char(N)

Answer: 'N'. A Char uses single quotes, like 'N'; double quotes make a String.

How do you turn an Int into a Double in Kotlin?

  • It happens automatically
  • Cast with (Double)
  • Call .toDouble() explicitly
  • Add 0.0 silently

Answer: Call .toDouble() explicitly. Kotlin avoids implicit widening; call .toDouble() explicitly.

What is the value and type of val x = 5?

  • A String "5"
  • A Double 5.0
  • A function returning 5
  • An Int with value 5

Answer: An Int with value 5. An integer literal is inferred as Int.

What is idiomatic Kotlin's default choice between val and var?

  • Prefer val, use var only when reassignment is needed
  • Always use var
  • Always use const
  • It does not matter

Answer: Prefer val, use var only when reassignment is needed. Default to val to signal that the value will not change.

What do underscores do in a number like 1_000_000?

  • Cause a compile error
  • Are ignored but make big numbers readable
  • Multiply the value
  • Convert it to a String

Answer: Are ignored but make big numbers readable. Underscores are visual separators the compiler ignores.