-
Notifications
You must be signed in to change notification settings - Fork 24
Using (val) and (const val) in Kotlin
Devrath edited this page Feb 10, 2024
·
2 revisions
- We use
valto declare read-only data in kotlin. - It is also called
assign oncevalues. - You should always strive to use
valto avoidside-effectsin kotlin that would lead to bugs.
- We use
const valto declare the constants in kotlin. - If we have a value that never changes and the value is known at compile time, we should strive to use
const val. - We can add only
primitive typeorstringsin theconst val. -
const valin kotlin is equivalent tostatic finalin Java.
- Using the
const valhelps the compiler to do optimizations during the compile time itself. - Also it's good to know that there is a value that does not change even during the runtime.
