Skip to content
tezvyn:

Kotlin Variables: `val` for Constants, `var` for Variables

Source: kotlinlang.orgEasyHow cards are made

Kotlin Variables: `val` for Constants, `var` for Variables

In Kotlin, val creates a read-only constant you assign once, like a fixed setting. var creates a mutable variable you can change later. Always prefer val unless you explicitly need to reassign a value to prevent accidental state changes.

Why it exists

Programs need to store and manage data. Kotlin provides two ways to declare variables to make code safer and more predictable. By distinguishing between data that can change (mutable) and data that cannot (immutable), the language helps developers prevent common bugs caused by unintended modifications.

The mental model

Think of val as a value written in permanent ink. Once you assign it, it's fixed and cannot be changed. Think of var as a value written on a whiteboard; you can erase it and write a new value at any time. This distinction forces you to be intentional about which data in your program is allowed to change.

How it works

You declare a variable using either the val keyword for read-only variables or var for mutable ones. The syntax is the keyword, followed by the variable name, an optional type, and the assigned value. For example: val pi = 3.14 or var counter = 0. Kotlin's type inference often allows you to omit the type (like :Double or :Int) if the value is assigned at declaration. If you declare a variable without initializing it, you must specify its type, like val name: String. You can then assign a value to name later, but only once since it's a val.

When to use it

Use val as your default for any value that will not change after its initial assignment. This is common for configuration settings, API response data, or properties of an object that are set at creation. Use var only when you have a clear need for a value to be reassigned, such as a loop counter, an accumulator that totals up values, or a property that tracks a changing state like a user's current score.

When not to use it

Avoid using var when val would suffice. This is a common beginner mistake. Using var everywhere makes the code harder to reason about, as any variable could potentially change anywhere in the code. This increases cognitive load and the risk of bugs. Always start with val and only change it to var if the compiler tells you a reassignment is needed and it makes logical sense.

One canonical example

Imagine tracking a user's score in an app. The app's name is a constant, but the score changes. You would define the name with val and the score with var. For example: val appName = "BrainBites" and var currentScore = 0. Later, you can update the score with currentScore = 100. If you tried to change appName, the compiler would give you an error because a val cannot be reassigned.

Interview question

A developer needs to store a user's current score in a game, which will change throughout gameplay. Which Kotlin keyword should be used?

  • a.let
  • b.val
  • c.varCorrect
  • d.const
Why?

The card states that 'var' should be used for values that need to be reassigned, such as a user's score, while 'val' is for values that remain constant after initial assignment. Choosing 'val' here would lead to a compilation error when trying to update the score.

Just read this? Test yourself on what you have been reading.

Read the original → kotlinlang.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.

See open roles