F
F
foonfyrick2020-11-20 16:40:27
Kotlin
foonfyrick, 2020-11-20 16:40:27

Val vs const val, I know that const val doesn't have a getter, what are the other differences?

Well, there is no getter for const val, is it very critical not to use val as a constant? How bad is it to use val as a constant?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
illuzor, 2020-11-20
@foonfyrick

const are compile time constants.
After compilation, they disappear (they are not in the bytecode) and are substituted for the place of use. This allows, for example, to use them in annotations.
Also const is evaluated at compile time.
If you write like this:

const val SUM = 1 + 2
someFunctionCall(SUM)

After compilation it will look like this:
someFunctionCall(3)

V
Vasily Bannikov, 2020-11-20
@vabka

const valis a compile-time constant. Its value will be the same for all
val- an immutable property (does not have a setter). Well, maybe this property - it is tied to a specific instance.
https://kotlinlang.ru/docs/reference/properties.html
const val and valare two different tools for different tasks.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question