+ 1
Extremely basic question
Hi, I just started on the Kotlin course and have a question. I understand that variable defines a value that is mutable and val defines a value that is immutable but I'm confused as to know which to use for what situation. What are some examples of the correct usage of these?
2 Answers
+ 6
Basically, you want a value to be mutable if you are going to change it and unmutable if you are not going to change it.
+ 2
For example, if you want to use the number Pi, you'd make it immutable because:
A) it's a value that is fixed (i.e 3.14) and not going to change.
B) by making it immutable, you're preventing yourself from accidentally changing it.