Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Kotlin
Kotlineasy

`val` vs `var` — what’s the difference?

Tags
#val#var#immutability
Back to categoryPractice quiz

Answer

`val` is a read-only reference (cannot be reassigned), while `var` is mutable (can be reassigned). `val` does not automatically make the object immutable.

Advanced answer

Deep dive

`val` means the reference can’t be reassigned after initialization. `var` means you can point it to a different value later.

That’s about the *reference*, not the object:

  • `val list = mutableListOf(1)` still allows `list.add(2)`.

Practical guidance

  • Prefer `val` by default (clearer intent, fewer accidental reassignments).
  • Use `var` only when reassignment is part of the logic.

Example

val xs = mutableListOf(1)
xs.add(2)      // OK
// xs = mutableListOf(3) // not OK

var n = 1
n = 2          // OK

Common pitfalls

  • Confusing `val` with deep immutability.
  • Using `var` “just in case” and making code harder to reason about.

Related questions

Kotlin
List vs MutableList in Kotlin: what’s the difference?
#kotlin#collections#immutability
Kotlin
What is a data class in Kotlin and why use it?
#data-class#kotlin#dto
Kotlin
Val vs Var?
#kotlin#variables#immutability
Java
Java `record`: what does it generate and when is it a good fit?
#java#record#dto
Java
`List.of(...)`: what kind of list does it create and a common gotcha?
#java#collections#immutability