InterviewSolution
| 1. |
Which one is better to use - val mutableList or var immutableList in the context of Kotlin? |
|
Answer» The PROGRAM's design clarity is improved by using mutable and immutable lists. This is done to have the developer think about and clarify the collection's purpose. We use a mutable list if the collection will alter as part of the design. On the other HAND, we use an immutable list if the model is only meant to be viewed. Val and var serve a distinct purpose than immutable and mutable lists. The val and var keywords specify how a variable's value/reference should be handled. We use var when the value or reference of a variable can be altered at any moment. On the other hand, we use val when a variable's value/reference can only be assigned once and cannot be modified later in the execution. Immutable lists are frequently preferred for a variety of reasons:
However, there are some disadvantages of using immutable lists as well. They are as follows :
|
|