Extremely basic question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

2nd Nov 2021, 11:20 PM
McKenzie Craps
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.
3rd Nov 2021, 2:21 AM
Paul K Sadler
Paul K Sadler - avatar
+ 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.
4th Nov 2021, 8:10 AM
Eli Levi