0
0
Kotlinprogramming~10 mins

Mutable vs immutable interfaces in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Mutable vs immutable interfaces
Start with collection reference
Is interface mutable?
NoImmutable: no change allowed
Yes
Mutable: can add, remove, update
Use collection methods accordingly
End
This flow shows how a collection reference can be either mutable or immutable, affecting what operations are allowed.
Execution Sample
Kotlin
val list: List<Int> = listOf(1, 2, 3)
val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)

// list.add(4) // Error
mutableList.add(4)
println(mutableList)
This code shows an immutable list that cannot be changed and a mutable list that can be updated.
Execution Table
StepCode LineActionResultOutput
1val list: List<Int> = listOf(1, 2, 3)Create immutable listlist = [1, 2, 3]
2val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)Create mutable listmutableList = [1, 2, 3]
3// list.add(4) // ErrorAttempt to add to immutable listCompile error: Unresolved reference: add
4mutableList.add(4)Add 4 to mutableListmutableList = [1, 2, 3, 4]
5println(mutableList)Print mutableList[1, 2, 3, 4]
💡 Execution stops after printing the updated mutable list; adding to immutable list is not allowed.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
listundefined[1, 2, 3][1, 2, 3][1, 2, 3]
mutableListundefined[1, 2, 3][1, 2, 3, 4][1, 2, 3, 4]
Key Moments - 2 Insights
Why can't we add an element to 'list'?
'list' is declared as List<Int>, which is immutable. The execution_table row 3 shows a compile error when trying to add.
What happens when we add an element to 'mutableList'?
Since 'mutableList' is MutableList<Int>, it allows adding elements. Row 4 shows the list updated with the new element.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what happens when trying to add to 'list'?
ACompile error occurs
BThe element is added successfully
CNothing happens, code skips
DRuntime exception occurs
💡 Hint
Check execution_table row 3 under 'Result' for the error message.
At which step does 'mutableList' change its contents?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at variable_tracker for 'mutableList' changes after each step.
If 'list' was declared as MutableList<Int>, what would happen at step 3?
ACompile error still occurs
BRuntime exception occurs
CElement would be added successfully
DCode would skip adding
💡 Hint
Refer to the difference between List and MutableList in the concept_flow and execution_table.
Concept Snapshot
Mutable vs Immutable Interfaces in Kotlin:
- List<T> is immutable: no add/remove/update allowed.
- MutableList<T> is mutable: supports add, remove, update.
- Immutable interfaces prevent accidental changes.
- Use mutable interfaces when you need to change collections.
- Trying to modify immutable collections causes compile errors.
Full Transcript
This lesson shows the difference between mutable and immutable interfaces in Kotlin using lists. An immutable list (List<Int>) cannot be changed after creation, so trying to add elements causes a compile error. A mutable list (MutableList<Int>) allows adding, removing, and updating elements. The execution table traces each step: creating lists, attempting to add to immutable list (which fails), adding to mutable list (which succeeds), and printing the result. The variable tracker shows how the lists' contents change over time. Key moments clarify why immutable lists cannot be changed and how mutable lists work. The quiz tests understanding of these concepts by referencing the execution steps. Remember: use immutable interfaces for safety and mutable interfaces when changes are needed.