0
0
Kotlinprogramming~10 mins

Constant values with const val in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constant values with const val
Declare const val
Assign compile-time value
Use constant in code
Value is inlined at compile time
Cannot change value later
Program runs using constant
This flow shows how a constant is declared with const val, assigned a fixed value, used in code, and remains unchanged during program execution.
Execution Sample
Kotlin
const val MAX_AGE = 100
fun main() {
  println("Max age is $MAX_AGE")
}
This code declares a constant MAX_AGE and prints it.
Execution Table
StepActionEvaluationResult
1Declare const val MAX_AGE = 100MAX_AGE assigned 100 at compile timeMAX_AGE = 100
2Enter main functionReady to execute printlnNo change
3Evaluate println("Max age is $MAX_AGE")Substitute MAX_AGE with 100Prints: Max age is 100
4Program endsNo further actionsExecution stops
💡 Program ends after printing the constant value
Variable Tracker
VariableStartAfter Step 1After Step 3Final
MAX_AGEundefined100 (constant)100 (used in print)100 (unchanged)
Key Moments - 2 Insights
Why can't we change the value of MAX_AGE after declaring it with const val?
Because const val defines a compile-time constant, its value is fixed and inlined during compilation, so changing it later is not allowed (see execution_table step 1).
Is const val the same as a regular val in Kotlin?
No, const val must be assigned a compile-time constant and is inlined, while val can be assigned at runtime and is not inlined (refer to execution_table step 1 vs step 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of MAX_AGE after step 1?
Anull
Bundefined
C100
DCannot be determined
💡 Hint
Check the 'Result' column in execution_table row for step 1
At which step does the program print the constant value?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for printing
If we try to assign MAX_AGE = 200 after declaration, what happens?
ACompile-time error
BValue changes to 200
CRuntime error
DValue changes but warning shown
💡 Hint
Recall that const val values cannot be changed after compile-time assignment (see key_moments)
Concept Snapshot
const val declares a compile-time constant in Kotlin
Value must be assigned immediately and be a constant expression
Value is inlined where used, no runtime overhead
Cannot be changed after declaration
Used for fixed values like MAX_AGE = 100
Full Transcript
This visual execution trace shows how Kotlin's const val works. First, a constant MAX_AGE is declared and assigned the value 100 at compile time. This value is fixed and cannot be changed later. When the main function runs, it prints the message including MAX_AGE, substituting the constant value directly. The program then ends. Key points include that const val values are inlined and immutable, different from regular val. The quizzes test understanding of when the value is assigned, when it is printed, and what happens if you try to change it.