Deep Kotlin tutorial covering val, var, const val, read-only reference with examples, diagram, mistakes, and interview answers.
val, var, and const val
What is val, var, and const val?
val, var, and const val is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: val, var, const val, read-only reference, compile-time constant. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
Why students should learn it
- Understand val, var, and const val in simple language
- Use val in real Kotlin or Android code
- Explain val, var, and const val in interviews with examples and trade-offs
How it works
val, var, and const val: val, var, const val, read-only reference, compile-time constant works through Kotlin's core syntax rules. The compiler infers types where it can, expressions such as if and when can produce a value directly instead of only running a side effect, and functions are written to be small and named so the intent is obvious from the signature without reading the body.
- Identify what problem val, var, and const val: val, var, const val, read-only reference, compile-time constant solves.
- Write the smallest Kotlin snippet that demonstrates it.
- Add one Android or JVM use case where it matters.
- Compare it with a similar concept so the difference is clear.
- Finish by naming one common mistake and the safer alternative.
Interview tip: keep the answer simple first, then add example, trade-off, and mistake.
Types / Classification
- val: core concept in this lesson
- var: core concept in this lesson
- const val: core concept in this lesson
- read-only reference: core concept in this lesson
- compile-time constant: core concept in this lesson
- Statement: performs an action without producing a value
- Expression: if/when/try can produce a value directly
- Declaration: val/var/fun/class introduces a new name
Important differences
| Concept 1 | Concept 2 | How to remember it |
|---|---|---|
| if statement | if expression | A statement just runs code; an expression also produces a value you can assign or return. |
| Positional argument | Named argument | Positional relies on order; named argument is explicit and safer with many parameters. |
| Function | Lambda | A function has a name and is called by that name; a lambda is an anonymous function value you can pass around. |
Diagram
flowchart TD
A["1.1 Kotlin Language Fundamentals"] --> B["val, var, and const val"]
B --> C["Why it exists"]
C --> D["How it works in Kotlin"]
D --> E["Android or JVM example"]
E --> F["Mistakes and interview answer"]
Examples
Named and default parameters
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")
when as an expression
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}
When to use it
Use it when: Reach for this when it makes your code clearer, safer, or easier for the next developer to follow.
Avoid it when: Skip it when a simpler, more familiar approach already solves the problem without extra ceremony.
Common mistakes
- Writing long positional parameter lists instead of using named arguments for clarity
- Using var when the value never actually changes
- Copy-pasting if/else chains instead of a single when expression
- Ignoring type inference and writing redundant explicit types everywhere
- Writing a multi-line lambda where a named function would read better
Interview questions
Read each answer in four parts: simple answer, code example, trade-off, and common mistake.
1. What is val, var, and const val in 1.1 Kotlin Language Fundamentals?
Answer: val, var, and const val is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: val, var, const val, read-only reference, compile-time constant. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas.
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
2. How does val, var, and const val work in Kotlin or Android code?
Answer: val, var, and const val: val, var, const val, read-only reference, compile-time constant works through Kotlin's core syntax rules. The compiler infers types where it can, expressions such as if and when can produce a value directly instead of only running a side effect, and functions are written to be small and named so the intent is obvious from the signature without reading the body.
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Using var when the value never actually changes
3. What is one common mistake with val, var, and const val?
Answer: A common mistake is: Writing long positional parameter lists instead of using named arguments for clarity Another is: Using var when the value never actually changes
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
4. How would you explain val, var, and const val with a small example?
Answer: val, var, and const val is an important Kotlin topic from 1.1 Kotlin Language Fundamentals. In this lesson we focus only on the concepts that belong together: val, var, const val, read-only reference, compile-time constant. This keeps the lesson deep enough for interviews but not mixed with unrelated ideas. Here is a short example that shows it in practice.
fun greet(name: String, greeting: String = "Hello"): String {
return "$greeting, $name!"
}
greet(name = "Asha")- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Using var when the value never actually changes
5. What is the trade-off of using val, var, and const val?
Answer: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
fun levelLabel(score: Int): String = when {
score >= 90 -> "Advanced"
score >= 60 -> "Intermediate"
else -> "Beginner"
}- Trade-off: if statement and if expression solve related but different problems: A statement just runs code; an expression also produces a value you can assign or return.
- Common mistake: Writing long positional parameter lists instead of using named arguments for clarity
Practice task
Write one Kotlin example for val, var, and const val. Then explain it using: what is it, how it works, where it is used, mistake to avoid, and one interview answer.