Why Kotlin use the word “val” to stand for constant?


I really want to know why Kotlin use the word val to stand for constant?

If var means variable.val means what ? val means var + l or val is only one word for short?

In the Kotlin Documentation,we only have the following words.

Read-only local variables are declared using val keyword. Mutable
local variables are declared using var keyword.

I have searched a lot by Google and the forum here, but I found nothing helpful.

First I asked this question at StackOverflow:



A val is not constant; you can get different values at different times. It is just that you cannot set the value, which means it is read-only to you (as the documentation says).

val is short for value.


If the variable declared as val is a simple type (int, float, boolean, etc.) val has the effect of const. However, if the variable holds the reference to an object that has sub-object(s), only the reference to the object is const. Variables inside the object and its sub-object can be changed. Hence, things are not constant.

Okay, with this I’m just repeating what jstuyts already said. Just to make sure the point is understood.

class MutableVal{
    val rand = Random()
    val t get() = rand.nextInt()

t is a mutable readonly


Because val is short, and you’re going to type it A LOT. :wink:


However your example with a val property is a different thing than a local variable (or should I say value?). A local val is actually immutable (what doesn’t mean that the referenced is immutable, too).


In Kotlin 1.0 perhaps, but not in Kotlin 1.1 and later with the addition of local delegated properties:

class MutableLocalVal {
    val delegate = object : ReadOnlyProperty<Nothing?, Int>
        val rand = Random()

        override fun getValue(thisRef: Nothing?, property: KProperty<*>) = rand.nextInt()

    fun foo()
        val localVal by delegate

        (1..10).forEach { println(localVal) }