Avatar ·

What is the difference between var and val in Kotlin? When to use which?

Hello! I just started learning Kotlin after Java and got confused about variable declarations. In Java, I'm used to declaring variables with a type (int, String) and a modifier (final for constants). In Kotlin, I see two keywords: var and val. I read that val is an analog of final in Java, and var is a regular mutable variable. But in practice, I have questions. For example, I wrote this code: ```kotlin fun main() { var name = "Anna" name = "Maria" // this works, fine val age = 25 age = 30 // Error! Val cannot be reassigned } ``` With val it's clear — you can't reassign. But what if val stores not a primitive but an object? For example: ```kotlin val list = mutableListOf("a", "b") list.add("c") // does this work? println(list) // prints [a, b, c] ```