Bird
Raised Fist0
Kotlinprogramming~10 mins

Val for immutable references in Kotlin - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Val for immutable references
Declare val x = 10
↓
x holds 10
↓
Try to reassign x = 20?
No→Error: val cannot be reassigned
This flow shows declaring a val variable, holding a value, and that reassignment is not allowed.
Execution Sample
Kotlin
val x = 10
println(x)
x = 20 // Error
Declare an immutable val x with 10, print it, then try to reassign (which causes error).
Execution Table
StepActionVariableValueResult
1Declare val x = 10x10x holds 10
2Print xx10Output: 10
3Attempt x = 20x10Error: val cannot be reassigned
💡 Execution stops at step 3 due to reassignment error on val variable.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
xundefined101010 (unchanged, reassignment failed)
Key Moments - 2 Insights
Why can't we assign a new value to a val variable after declaration?
Because val declares an immutable reference, so reassignment is not allowed as shown in execution_table step 3.
Does val mean the value itself is immutable or just the reference?
val means the reference cannot change to point to another object, but the object itself can be mutable if its type allows it.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after step 2?
A10
B20
Cundefined
DError
💡 Hint
Check the 'Value' column in execution_table row for step 2.
At which step does the program stop due to an error?
AStep 1
BStep 2
CStep 3
DNo error
💡 Hint
Look at the 'Result' column in execution_table for error messages.
If x was declared with var instead of val, what would happen at step 3?
AError: val cannot be reassigned
Bx would be updated to 20
Cx would remain 10
DProgram would crash
💡 Hint
Recall that var allows reassignment, unlike val, as explained in key_moments.
Concept Snapshot
val declares an immutable reference in Kotlin.
You assign it once at declaration.
Trying to reassign causes a compile-time error.
val does not guarantee the object is immutable, only the reference.
Use val for constants or references that should not change.
Full Transcript
In Kotlin, val is used to declare an immutable reference. When you write val x = 10, x holds the value 10 and cannot be reassigned. If you try to assign x = 20 later, the compiler gives an error because val variables cannot be reassigned. The value held by x remains 10 throughout. This means val protects the reference from changing, but if the object itself is mutable, its contents can still change. This example shows declaring val, printing its value, and the error when trying to reassign. Remember, use val when you want a fixed reference.

Practice

(1/5)
1. What does the val keyword do in Kotlin?
easy
A. Defines a class
B. Creates an immutable reference that cannot be reassigned
C. Declares a function
D. Creates a mutable variable that can be reassigned

Solution

  1. Step 1: Understand the role of val

    val declares a variable whose reference cannot be changed after assignment.
  2. Step 2: Compare with other keywords

    var allows reassignment, but val does not.
  3. Final Answer:

    Creates an immutable reference that cannot be reassigned -> Option B
  4. Quick Check:

    val = immutable reference [OK]
Hint: Remember: val means fixed reference, var means changeable [OK]
Common Mistakes:
  • Confusing val with var
  • Thinking val makes the object itself immutable
  • Assuming val declares a function or class
2. Which of the following is the correct syntax to declare an immutable integer variable named count with value 10 in Kotlin?
easy
A. val count = 10
B. var count = 10
C. int val count = 10
D. val count : Int =

Solution

  1. Step 1: Identify correct keyword and syntax

    val is used for immutable variables, and Kotlin infers type if not specified.
  2. Step 2: Check each option

    var count = 10 uses var (mutable), C has invalid syntax, D is incomplete.
  3. Final Answer:

    val count = 10 -> Option A
  4. Quick Check:

    Immutable variable syntax = val count = 10 [OK]
Hint: Use val for fixed values, var for changeable ones [OK]
Common Mistakes:
  • Using var instead of val for immutable variables
  • Incorrect Kotlin type declarations
  • Leaving declarations incomplete
3. What will be the output of the following Kotlin code?
val list = mutableListOf(1, 2, 3)
list.add(4)
println(list)
medium
A. Runtime error
B. [1, 2, 3]
C. Compilation error
D. [1, 2, 3, 4]

Solution

  1. Step 1: Understand val with mutable objects

    val means the reference to the list cannot change, but the list itself can be modified.
  2. Step 2: Analyze code behavior

    Adding 4 to the mutable list changes its content, so printing shows [1, 2, 3, 4].
  3. Final Answer:

    [1, 2, 3, 4] -> Option D
  4. Quick Check:

    val reference + mutable object = modified list [OK]
Hint: val fixes reference, not object content if mutable [OK]
Common Mistakes:
  • Thinking val prevents modifying the list content
  • Expecting compilation or runtime errors
  • Confusing immutable reference with immutable object
4. Identify the error in the following Kotlin code:
val number = 5
number = 10
println(number)
medium
A. No error, prints 10
B. Runtime error: reassignment not allowed
C. Compilation error: val cannot be reassigned
D. Syntax error: missing semicolon

Solution

  1. Step 1: Check reassignment of val variable

    val variables cannot be reassigned after initialization.
  2. Step 2: Identify error type

    Trying to assign 10 to number causes a compilation error.
  3. Final Answer:

    Compilation error: val cannot be reassigned -> Option C
  4. Quick Check:

    val reassignment = compilation error [OK]
Hint: val variables cannot change after first assignment [OK]
Common Mistakes:
  • Assuming val variables can be reassigned
  • Confusing runtime errors with compile errors
  • Ignoring Kotlin's no-semicolon rule
5. Given the following Kotlin code, what will be the output?
val map = mutableMapOf("a" to 1, "b" to 2)
map["a"] = 3
println(map)

Explain why this works even though map is declared with val.
hard
A. {a=3, b=2}
B. {a=1, b=2}
C. Compilation error: cannot modify val map
D. Runtime error

Solution

  1. Step 1: Understand val with mutable collections

    val means the reference to the map cannot change, but the map's content can be modified.
  2. Step 2: Analyze the map update

    Updating key "a" to 3 changes the map content, so printing shows {a=3, b=2}.
  3. Final Answer:

    {a=3, b=2} -> Option A
  4. Quick Check:

    val reference + mutable map = updated map content [OK]
Hint: val fixes reference, mutable map content can change [OK]
Common Mistakes:
  • Thinking val prevents modifying map content
  • Expecting compilation or runtime errors
  • Confusing immutable reference with immutable object
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard