Start learning free

LESSON 13 OF 348 · VARIABLES

Constants in Kotlin: val and const val

Lesson Read the full explanation

There is a special type of variable in Kotlin, which is called constant or val.

A constant is a variable that we do not want to change the value of during the whole program.


Creation:

You create a constant variable just like you create a normal one, but using the keyword val instead of var:

val ourName = "AlgoCademy"

println(ourName) // Output: AlgoCademy

Reassignment not allowed:

However, a val variable cannot be reassigned because it is constant. If you try to reassign a val variable, you'll get an error.

For example, this code:

val ourName = "AlgoCademy"

ourName = "Andy" // Here is the problem

would produce error: val cannot be reassigned


Usage:

It seems like constants are just variables with some limitations. So, why use them when we can do the same things (and more) with normal variables?

The power of constants lies in their limitation! Constants were invented to provide some level of guarantee that the code can't change the underlying value.

This is not of much importance for a smaller project, but matters on a larger project with multiple components written by multiple authors.

Picture this: You are the first developer to have ever worked at a new startup. When you have written the core code, you created some important variables that describe your company:

val companyName = "Apple"
val foundingDate = "April 1st, 1976"
val founderName = "Steve Jobs"

Ten years later, your company blew up and you have hundreds of Software Engineers managing millions of lines of code.

I bet you wouldn't love it if a Junior Developer was able to do this successfully:

companyName = "Microsoft"
founderName = "Bill Gates"

But you are smart! You made those variables constants ten years ago and have nothing to worry about now. As for the Junior, they'll just get an error and probably a slap on the wrist by their manager.


Assignment
Follow the Coding Tutorial and let's work with constants!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · VARIABLES

    Constants in Kotlin, explained

    What a constant is

    A constant is a name whose value is set once and then never changes while the program runs. In Kotlin you create one by declaring the variable with val instead of var:

    val ourName = "AlgoCademy"
    
    println(ourName) // AlgoCademy
    

    Reading the value is no different from reading any variable. The only restriction is on writing: a name declared with val can't be reassigned.

    In Kotlin's own vocabulary, val declares a read-only name. People often call it a constant in everyday speech, as this lesson does. The distinction matters in one place, covered below.

    What happens when you try to change it

    val ourName = "AlgoCademy"
    
    ourName = "Andy"
    

    The compiler refuses to build the program:

    error: 'val' cannot be reassigned.
        ourName = "Andy"
        ^^^^^^^
    

    The error comes before the program runs, so there is no moment where the wrong value is used. Compound forms such as ourName += "x" are reassignments too and fail the same way.

    Why limit yourself?

    A constant looks like a variable with a handicap, so why use it? Because the handicap is a guarantee. Imagine a large project where hundreds of people edit millions of lines. Early on, someone wrote:

    val companyName = "Apple"
    val foundingDate = "April 1st, 1976"
    val founderName = "Steve Jobs"
    

    Years later, a careless change somewhere else tries companyName = "Microsoft". With val, that line doesn't compile, and the mistake is found immediately and at the exact line. With var, it would silently change what every other part of the program reads.

    When you read code, val also tells you something: you don't have to scan the rest of the function to see whether the value changes. It doesn't. That is why Kotlin programmers declare names with val by default and switch to var only when a value must change.

    A val does not have to be known when you write the code. Its value may be computed when the program runs, and it is still assigned only once:

    val price = 20
    val tax = price * 5 / 100
    val total = price + tax
    println(total) // 21
    

    Each of the three names receives its value exactly once. None of them can be changed afterwards, but each can be read as often as you like.

    The exercise

    The starter code reassigns a constant:

    val pi = 3.14
    
    pi = 5
    
    println(pi)
    

    The compiler's first complaint is 'val' cannot be reassigned, pointing at pi = 5. It may add more messages about the line, for example that the integer 5 doesn't fit a Double. Those all come from the same line. The task asks for the program to print 3.14, and the lesson gives the fix: delete the line that reassigns pi. The declaration and the println stay unchanged:

    val pi = 3.14
    
    
    
    println(pi)
    

    It prints 3.14. The other possible fix, changing val to var, would make the assignment legal but print 5 and defeat the point of a constant. When the compiler rejects a reassignment, first ask whether the reassignment was meant to happen.

    What val does not mean

    • It fixes the name, not the object. A val holding a mutable list can't be pointed at another list, but the list can still be changed: val list = mutableListOf(1, 2) followed by list.add(3) is fine and gives [1, 2, 3]. A plain number or string can't be modified, so for those val fixes the value completely.
    • A delayed first assignment is allowed. Inside a function, val x: Int followed later by x = 4 works once, as long as the compiler can see it happens once. A second assignment is an error.
    • const val is the compile-time kind. A top-level const val MAX_USERS = 100 is substituted at compile time and may only hold a number, a string or another basic type. It can't go inside a function: the compiler says modifier 'const' is not applicable to 'local variable'. Constants written this way are conventionally named in capital letters.

    Key takeaways

    • val creates a name that can be assigned only once.
    • Reassigning it is a compile error: 'val' cannot be reassigned.
    • The restriction is useful: it guards against accidental changes and makes code easier to read.
    • Use val by default and var when the value must change.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free