Junior — Middle
Mis on deklaratsioonide eesmärk, kasutades võtmesõnu 'const' ja 'val' Kotlinis?
sobes.tech AI
Vastus AI-lt
In Kotlin, the keywords const and val are used to declare immutable values, but with different characteristics:
-
const— are compile-time constants. They must be primitive types or strings and are declared at the object or file level. The value ofconstcannot be changed and is known during compilation. -
val— are runtime immutable variables. The value is assigned once during initialization and cannot be changed, but can be computed during execution.
Example:
const val MAX_COUNT = 100 // compile-time constant
val currentTime = System.currentTimeMillis() // immutable value at runtime