Prefer val Over var
It may seem that var
is more useful than val
. After all, a val
can only hold one value, whereas we can reassign values to a var
as needed.
Yet you will find that the vast majority of Kotlin code uses val
. If anything, var
tends to be considered as a “workaround”, for cases where val
cannot be used for one reason or another.
We will hold off on a detailed explanation of this philosophy, and we will return to this point later in the book. For the moment, take it on faith that we have good reasons to prefer val
over var
, and that is why this book will be using val
much more than var
.
Prev Table of Contents Next
This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.