Immutability means values cannot change once created. This helps avoid mistakes and makes programs easier to understand and safe.
When you want to keep data safe from accidental changes.
When multiple parts of a program use the same data without interfering.
When you want your program to be easier to test and debug.
When working with multiple threads to avoid conflicts.
When you want to write clear and predictable code.
Syntax
Kotlin
val name: String = "Alice"// Immutable variablevar age: Int = 30// Mutable variable
val creates an immutable variable that cannot be changed after assignment.
var creates a mutable variable that can be changed later.
Examples
You cannot change a val once set.
Kotlin
val greeting = "Hello"// greeting = "Hi" // This will cause a compile error
var allows changing the value.
Kotlin
var counter = 0
counter = 1// This is allowed because counter is mutable
Immutable collections cannot be changed after creation.
Kotlin
val list = listOf(1, 2, 3)
// list.add(4) // Error: list is immutable
Mutable collections can be changed.
Kotlin
val mutableList = mutableListOf(1, 2, 3)
mutableList.add(4) // This works because the list is mutable
Sample Program
This program shows how val and var work. Trying to change val causes an error, but var can be changed.
Kotlin
fun main() {
val name = "Bob"// name = "Alice" // Uncommenting this line causes an errorvar age = 25println("Name: $name, Age: $age")
age = 26// Changing mutable variableprintln("Updated Age: $age")
}
OutputSuccess
Important Notes
Using val by default helps prevent bugs caused by unexpected changes.
Immutability makes your code easier to read and reason about.
Mutable variables (var) should be used only when necessary.
Summary
Immutability means values cannot change after creation.
Kotlin uses val for immutable and var for mutable variables.
Using immutability by default makes code safer and easier to understand.
Practice
(1/5)
1. Why does Kotlin use val for variables by default instead of var?
easy
A. Because val creates immutable variables, making code safer and easier to understand.
B. Because val allows variables to change values anytime.
Kotlin uses val by default to create immutable variables that cannot be changed after assignment.
Step 2: Recognize benefits of immutability
Immutable variables prevent accidental changes, making code safer and easier to read and maintain.
Final Answer:
Because val creates immutable variables, making code safer and easier to understand. -> Option A
Quick Check:
Immutability = safer code [OK]
Hint: Remember: val means fixed value, var means variable value [OK]
Common Mistakes:
Thinking val allows value changes
Believing var is deprecated
Assuming val is faster than var
2. Which of the following Kotlin declarations correctly creates an immutable variable?
easy
A. var name = "Alice"
B. val name = "Alice"
C. immutable name = "Alice"
D. const var name = "Alice"
Solution
Step 1: Identify Kotlin syntax for immutable variables
Kotlin uses val to declare immutable variables that cannot be reassigned.
Step 2: Check each option
val name = "Alice" uses val correctly. var name = "Alice" uses var which is mutable. The other two options use invalid syntax.
Final Answer:
val name = "Alice" -> Option B
Quick Check:
Immutable variable syntax = val [OK]
Hint: val means immutable, var means mutable [OK]
Common Mistakes:
Using var instead of val for immutability
Trying to use 'immutable' keyword
Combining const and var incorrectly
3. What will be the output of this Kotlin code?
val x = 10
// x = 20
println(x)
medium
A. 20
B. Runtime error
C. 10
D. Compilation error
Solution
Step 1: Understand the code behavior
The variable x is declared with val, so it is immutable and cannot be reassigned.
Step 2: Analyze the commented reassignment
The reassignment x = 20 is commented out, so it does not run. The value of x remains 10.
Final Answer:
10 -> Option C
Quick Check:
Immutable val keeps value 10 [OK]
Hint: Commented code does not run, so value stays same [OK]
Common Mistakes:
Assuming commented code runs
Thinking val variables can be reassigned
Confusing compilation and runtime errors
4. Identify the error in this Kotlin code:
val count = 5
count = 10
println(count)
medium
A. Syntax error: missing semicolon
B. No error, prints 10
C. Runtime error: variable not initialized
D. Compilation error: val cannot be reassigned
Solution
Step 1: Check variable declaration
count is declared with val, making it immutable.
Step 2: Analyze reassignment
Trying to assign count = 10 causes a compilation error because val variables cannot be reassigned.
Final Answer:
Compilation error: val cannot be reassigned -> Option D
Quick Check:
val reassignment = compilation error [OK]
Hint: val variables cannot be reassigned after declaration [OK]
Common Mistakes:
Thinking val variables can be reassigned
Confusing runtime and compilation errors
Assuming semicolons are required in Kotlin
5. You want to create a list of user names that should never change after creation. Which Kotlin declaration is best and why?
val users = listOf("Anna", "Bob", "Cara")
hard
A. Use val with listOf because it creates an immutable list and reference.
B. Use val with mutableListOf to allow adding users later.
C. Use var with listOf because the list can be changed later.
D. Use var with mutableListOf to change the list and reference.
Solution
Step 1: Understand immutability of variable and list
val means the reference cannot change, and listOf creates an immutable list that cannot be modified.
Step 2: Compare options
Use val with listOf because it creates an immutable list and reference. uses val and listOf, ensuring both the reference and list contents cannot change, which matches the requirement.
Final Answer:
Use val with listOf because it creates an immutable list and reference. -> Option A
Quick Check:
val + listOf = fully immutable list [OK]
Hint: val + listOf = fixed list and reference [OK]
Common Mistakes:
Using var with mutableListOf when immutability is needed
Confusing mutableListOf with listOf
Thinking val allows list content changes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.