val mutableList vs var immutableList . When to use which in Kotlin?
val mutableList vs var immutableList . When to use which in Kotlin?
Here's an explanation of the differences between val mutableList
and var immutableList
in Kotlin, and when to use each:
val mutableList
val
mutableList is a mutable list that is declared using the val
keyword.Example:
val mutableList = mutableListOf(1, 2, 3)
mutableList.add(4) // Allowed
mutableList = emptyList() // Not allowed
var immutableList
var
immutableList is an immutable list that is declared using the var
keyword.Example:
var immutableList = listOf(1, 2, 3)
immutableList = emptyList() // Allowed
immutableList.add(4) // Not allowed
middle
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào